xref: /freebsd/sys/dev/sdhci/sdhci_acpi.c (revision 076ad2f836d5f49dc1375f1677335a48fe0d4b82)
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/conf.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/resource.h>
38 #include <sys/rman.h>
39 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
41 
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <machine/stdarg.h>
45 
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <dev/acpica/acpivar.h>
48 
49 #include <dev/mmc/bridge.h>
50 #include <dev/mmc/mmcreg.h>
51 #include <dev/mmc/mmcbrvar.h>
52 
53 #include "sdhci.h"
54 #include "mmcbr_if.h"
55 #include "sdhci_if.h"
56 
57 static const struct sdhci_acpi_device {
58 	const char*	hid;
59 	int		uid;
60 	const char	*desc;
61 	u_int		quirks;
62 } sdhci_acpi_devices[] = {
63 	{ "80860F14",	1,	"Intel Bay Trail SD Host Controller",
64 	    SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE |
65 	    SDHCI_QUIRK_INTEL_POWER_UP_RESET },
66 	{ "80860F14",	3,	"Intel Bay Trail SD Host Controller",
67 	    SDHCI_QUIRK_INTEL_POWER_UP_RESET },
68 	{ "80860F16",	0,	"Intel Bay Trail SD Host Controller",
69 	    0 },
70 	{ NULL, 0, NULL, 0}
71 };
72 
73 static char *sdhci_ids[] = {
74 	"80860F14",
75 	"80860F16",
76 	NULL
77 };
78 
79 struct sdhci_acpi_softc {
80 	u_int		quirks;		/* Chip specific quirks */
81 	struct resource *irq_res;	/* IRQ resource */
82 	void		*intrhand;	/* Interrupt handle */
83 
84 	struct sdhci_slot slot;
85 	struct resource	*mem_res;	/* Memory resource */
86 };
87 
88 static void sdhci_acpi_intr(void *arg);
89 static int sdhci_acpi_detach(device_t dev);
90 
91 static uint8_t
92 sdhci_acpi_read_1(device_t dev, struct sdhci_slot *slot, bus_size_t off)
93 {
94 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
95 
96 	bus_barrier(sc->mem_res, 0, 0xFF,
97 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
98 	return bus_read_1(sc->mem_res, off);
99 }
100 
101 static void
102 sdhci_acpi_write_1(device_t dev, struct sdhci_slot *slot, bus_size_t off,
103     uint8_t val)
104 {
105 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
106 
107 	bus_barrier(sc->mem_res, 0, 0xFF,
108 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
109 	bus_write_1(sc->mem_res, off, val);
110 }
111 
112 static uint16_t
113 sdhci_acpi_read_2(device_t dev, struct sdhci_slot *slot, bus_size_t off)
114 {
115 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
116 
117 	bus_barrier(sc->mem_res, 0, 0xFF,
118 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
119 	return bus_read_2(sc->mem_res, off);
120 }
121 
122 static void
123 sdhci_acpi_write_2(device_t dev, struct sdhci_slot *slot, bus_size_t off,
124     uint16_t val)
125 {
126 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
127 
128 	bus_barrier(sc->mem_res, 0, 0xFF,
129 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
130 	bus_write_2(sc->mem_res, off, val);
131 }
132 
133 static uint32_t
134 sdhci_acpi_read_4(device_t dev, struct sdhci_slot *slot, bus_size_t off)
135 {
136 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
137 
138 	bus_barrier(sc->mem_res, 0, 0xFF,
139 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
140 	return bus_read_4(sc->mem_res, off);
141 }
142 
143 static void
144 sdhci_acpi_write_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,
145     uint32_t val)
146 {
147 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
148 
149 	bus_barrier(sc->mem_res, 0, 0xFF,
150 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
151 	bus_write_4(sc->mem_res, off, val);
152 }
153 
154 static void
155 sdhci_acpi_read_multi_4(device_t dev, struct sdhci_slot *slot,
156     bus_size_t off, uint32_t *data, bus_size_t count)
157 {
158 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
159 
160 	bus_read_multi_stream_4(sc->mem_res, off, data, count);
161 }
162 
163 static void
164 sdhci_acpi_write_multi_4(device_t dev, struct sdhci_slot *slot,
165     bus_size_t off, uint32_t *data, bus_size_t count)
166 {
167 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
168 
169 	bus_write_multi_stream_4(sc->mem_res, off, data, count);
170 }
171 
172 static const struct sdhci_acpi_device *
173 sdhci_acpi_find_device(device_t dev)
174 {
175 	const char *hid;
176 	int i, uid;
177 	ACPI_HANDLE handle;
178 	ACPI_STATUS status;
179 
180 	hid = ACPI_ID_PROBE(device_get_parent(dev), dev, sdhci_ids);
181 	if (hid == NULL)
182 		return (NULL);
183 
184 	handle = acpi_get_handle(dev);
185 	status = acpi_GetInteger(handle, "_UID", &uid);
186 	if (ACPI_FAILURE(status))
187 		uid = 0;
188 
189 	for (i = 0; sdhci_acpi_devices[i].hid != NULL; i++) {
190 		if (strcmp(sdhci_acpi_devices[i].hid, hid) != 0)
191 			continue;
192 		if ((sdhci_acpi_devices[i].uid != 0) &&
193 		    (sdhci_acpi_devices[i].uid != uid))
194 			continue;
195 		return &sdhci_acpi_devices[i];
196 	}
197 
198 	return (NULL);
199 }
200 
201 static int
202 sdhci_acpi_probe(device_t dev)
203 {
204 	const struct sdhci_acpi_device *acpi_dev;
205 
206 	acpi_dev = sdhci_acpi_find_device(dev);
207 	if (acpi_dev == NULL)
208 		return (ENXIO);
209 
210 	device_set_desc(dev, acpi_dev->desc);
211 
212 	return (BUS_PROBE_DEFAULT);
213 }
214 
215 static int
216 sdhci_acpi_attach(device_t dev)
217 {
218 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
219 	int rid, err;
220 	const struct sdhci_acpi_device *acpi_dev;
221 
222 	acpi_dev = sdhci_acpi_find_device(dev);
223 	if (acpi_dev == NULL)
224 		return (ENXIO);
225 
226 	sc->quirks = acpi_dev->quirks;
227 
228 	/* Allocate IRQ. */
229 	rid = 0;
230 	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
231 		RF_ACTIVE);
232 	if (sc->irq_res == NULL) {
233 		device_printf(dev, "can't allocate IRQ\n");
234 		return (ENOMEM);
235 	}
236 
237 	rid = 0;
238 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
239 	    &rid, RF_ACTIVE);
240 	if (sc->mem_res == NULL) {
241 		device_printf(dev, "can't allocate memory resource for slot\n");
242 		sdhci_acpi_detach(dev);
243 		return (ENOMEM);
244 	}
245 
246 	sc->slot.quirks = sc->quirks;
247 
248 	err = sdhci_init_slot(dev, &sc->slot, 0);
249 	if (err) {
250 		device_printf(dev, "failed to init slot\n");
251 		sdhci_acpi_detach(dev);
252 		return (err);
253 	}
254 
255 	/* Activate the interrupt */
256 	err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
257 	    NULL, sdhci_acpi_intr, sc, &sc->intrhand);
258 	if (err) {
259 		device_printf(dev, "can't setup IRQ\n");
260 		sdhci_acpi_detach(dev);
261 		return (err);
262 	}
263 
264 	/* Process cards detection. */
265 	sdhci_start_slot(&sc->slot);
266 
267 	return (0);
268 }
269 
270 static int
271 sdhci_acpi_detach(device_t dev)
272 {
273 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
274 
275 	if (sc->intrhand)
276 		bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
277 	if (sc->irq_res)
278 		bus_release_resource(dev, SYS_RES_IRQ,
279 		    rman_get_rid(sc->irq_res), sc->irq_res);
280 
281 	if (sc->mem_res) {
282 		sdhci_cleanup_slot(&sc->slot);
283 		bus_release_resource(dev, SYS_RES_MEMORY,
284 		    rman_get_rid(sc->mem_res), sc->mem_res);
285 	}
286 
287 	return (0);
288 }
289 
290 static int
291 sdhci_acpi_shutdown(device_t dev)
292 {
293 
294 	return (0);
295 }
296 
297 static int
298 sdhci_acpi_suspend(device_t dev)
299 {
300 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
301 	int err;
302 
303 	err = bus_generic_suspend(dev);
304 	if (err)
305 		return (err);
306 	sdhci_generic_suspend(&sc->slot);
307 	return (0);
308 }
309 
310 static int
311 sdhci_acpi_resume(device_t dev)
312 {
313 	struct sdhci_acpi_softc *sc = device_get_softc(dev);
314 	int err;
315 
316 	sdhci_generic_resume(&sc->slot);
317 	err = bus_generic_resume(dev);
318 	if (err)
319 		return (err);
320 	return (0);
321 }
322 
323 static void
324 sdhci_acpi_intr(void *arg)
325 {
326 	struct sdhci_acpi_softc *sc = (struct sdhci_acpi_softc *)arg;
327 
328 	sdhci_generic_intr(&sc->slot);
329 }
330 
331 static device_method_t sdhci_methods[] = {
332 	/* device_if */
333 	DEVMETHOD(device_probe, sdhci_acpi_probe),
334 	DEVMETHOD(device_attach, sdhci_acpi_attach),
335 	DEVMETHOD(device_detach, sdhci_acpi_detach),
336 	DEVMETHOD(device_shutdown, sdhci_acpi_shutdown),
337 	DEVMETHOD(device_suspend, sdhci_acpi_suspend),
338 	DEVMETHOD(device_resume, sdhci_acpi_resume),
339 
340 	/* Bus interface */
341 	DEVMETHOD(bus_read_ivar,	sdhci_generic_read_ivar),
342 	DEVMETHOD(bus_write_ivar,	sdhci_generic_write_ivar),
343 
344 	/* mmcbr_if */
345 	DEVMETHOD(mmcbr_update_ios,	sdhci_generic_update_ios),
346 	DEVMETHOD(mmcbr_request,	sdhci_generic_request),
347 	DEVMETHOD(mmcbr_get_ro,		sdhci_generic_get_ro),
348 	DEVMETHOD(mmcbr_acquire_host,   sdhci_generic_acquire_host),
349 	DEVMETHOD(mmcbr_release_host,   sdhci_generic_release_host),
350 
351 	/* SDHCI registers accessors */
352 	DEVMETHOD(sdhci_read_1,		sdhci_acpi_read_1),
353 	DEVMETHOD(sdhci_read_2,		sdhci_acpi_read_2),
354 	DEVMETHOD(sdhci_read_4,		sdhci_acpi_read_4),
355 	DEVMETHOD(sdhci_read_multi_4,	sdhci_acpi_read_multi_4),
356 	DEVMETHOD(sdhci_write_1,	sdhci_acpi_write_1),
357 	DEVMETHOD(sdhci_write_2,	sdhci_acpi_write_2),
358 	DEVMETHOD(sdhci_write_4,	sdhci_acpi_write_4),
359 	DEVMETHOD(sdhci_write_multi_4,	sdhci_acpi_write_multi_4),
360 
361 	DEVMETHOD_END
362 };
363 
364 static driver_t sdhci_acpi_driver = {
365 	"sdhci_acpi",
366 	sdhci_methods,
367 	sizeof(struct sdhci_acpi_softc),
368 };
369 static devclass_t sdhci_acpi_devclass;
370 
371 DRIVER_MODULE(sdhci_acpi, acpi, sdhci_acpi_driver, sdhci_acpi_devclass, NULL,
372     NULL);
373 MODULE_DEPEND(sdhci_acpi, sdhci, 1, 1, 1);
374 DRIVER_MODULE(mmc, sdhci_acpi, mmc_driver, mmc_devclass, NULL, NULL);
375 MODULE_DEPEND(sdhci_acpi, mmc, 1, 1, 1);
376