2 * Copyright (c) 2017 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
32 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/resource.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <dev/acpica/acpivar.h>
47 #include <dev/mmc/bridge.h>
49 #include <dev/sdhci/sdhci.h>
54 static const struct sdhci_acpi_device {
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",
70 static char *sdhci_ids[] = {
76 struct sdhci_acpi_softc {
77 u_int quirks; /* Chip specific quirks */
78 struct resource *irq_res; /* IRQ resource */
79 void *intrhand; /* Interrupt handle */
81 struct sdhci_slot slot;
82 struct resource *mem_res; /* Memory resource */
85 static void sdhci_acpi_intr(void *arg);
86 static int sdhci_acpi_detach(device_t dev);
89 sdhci_acpi_read_1(device_t dev, struct sdhci_slot *slot, bus_size_t off)
91 struct sdhci_acpi_softc *sc = device_get_softc(dev);
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);
99 sdhci_acpi_write_1(device_t dev, struct sdhci_slot *slot, bus_size_t off,
102 struct sdhci_acpi_softc *sc = device_get_softc(dev);
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);
110 sdhci_acpi_read_2(device_t dev, struct sdhci_slot *slot, bus_size_t off)
112 struct sdhci_acpi_softc *sc = device_get_softc(dev);
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);
120 sdhci_acpi_write_2(device_t dev, struct sdhci_slot *slot, bus_size_t off,
123 struct sdhci_acpi_softc *sc = device_get_softc(dev);
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);
131 sdhci_acpi_read_4(device_t dev, struct sdhci_slot *slot, bus_size_t off)
133 struct sdhci_acpi_softc *sc = device_get_softc(dev);
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);
141 sdhci_acpi_write_4(device_t dev, struct sdhci_slot *slot, bus_size_t off,
144 struct sdhci_acpi_softc *sc = device_get_softc(dev);
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);
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)
155 struct sdhci_acpi_softc *sc = device_get_softc(dev);
157 bus_read_multi_stream_4(sc->mem_res, off, data, count);
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)
164 struct sdhci_acpi_softc *sc = device_get_softc(dev);
166 bus_write_multi_stream_4(sc->mem_res, off, data, count);
169 static const struct sdhci_acpi_device *
170 sdhci_acpi_find_device(device_t dev)
177 hid = ACPI_ID_PROBE(device_get_parent(dev), dev, sdhci_ids);
181 handle = acpi_get_handle(dev);
182 status = acpi_GetInteger(handle, "_UID", &uid);
183 if (ACPI_FAILURE(status))
186 for (i = 0; sdhci_acpi_devices[i].hid != NULL; i++) {
187 if (strcmp(sdhci_acpi_devices[i].hid, hid) != 0)
189 if ((sdhci_acpi_devices[i].uid != 0) &&
190 (sdhci_acpi_devices[i].uid != uid))
192 return (&sdhci_acpi_devices[i]);
199 sdhci_acpi_probe(device_t dev)
201 const struct sdhci_acpi_device *acpi_dev;
203 acpi_dev = sdhci_acpi_find_device(dev);
204 if (acpi_dev == NULL)
207 device_set_desc(dev, acpi_dev->desc);
209 return (BUS_PROBE_DEFAULT);
213 sdhci_acpi_attach(device_t dev)
215 struct sdhci_acpi_softc *sc = device_get_softc(dev);
217 const struct sdhci_acpi_device *acpi_dev;
219 acpi_dev = sdhci_acpi_find_device(dev);
220 if (acpi_dev == NULL)
223 sc->quirks = acpi_dev->quirks;
227 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
229 if (sc->irq_res == NULL) {
230 device_printf(dev, "can't allocate IRQ\n");
235 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
237 if (sc->mem_res == NULL) {
238 device_printf(dev, "can't allocate memory resource for slot\n");
239 sdhci_acpi_detach(dev);
243 sc->slot.quirks = sc->quirks;
245 err = sdhci_init_slot(dev, &sc->slot, 0);
247 device_printf(dev, "failed to init slot\n");
248 sdhci_acpi_detach(dev);
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);
256 device_printf(dev, "can't setup IRQ\n");
257 sdhci_acpi_detach(dev);
261 /* Process cards detection. */
262 sdhci_start_slot(&sc->slot);
268 sdhci_acpi_detach(device_t dev)
270 struct sdhci_acpi_softc *sc = device_get_softc(dev);
273 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
275 bus_release_resource(dev, SYS_RES_IRQ,
276 rman_get_rid(sc->irq_res), sc->irq_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);
288 sdhci_acpi_shutdown(device_t dev)
295 sdhci_acpi_suspend(device_t dev)
297 struct sdhci_acpi_softc *sc = device_get_softc(dev);
300 err = bus_generic_suspend(dev);
303 sdhci_generic_suspend(&sc->slot);
308 sdhci_acpi_resume(device_t dev)
310 struct sdhci_acpi_softc *sc = device_get_softc(dev);
313 sdhci_generic_resume(&sc->slot);
314 err = bus_generic_resume(dev);
321 sdhci_acpi_intr(void *arg)
323 struct sdhci_acpi_softc *sc = (struct sdhci_acpi_softc *)arg;
325 sdhci_generic_intr(&sc->slot);
328 static device_method_t sdhci_methods[] = {
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),
338 DEVMETHOD(bus_read_ivar, sdhci_generic_read_ivar),
339 DEVMETHOD(bus_write_ivar, sdhci_generic_write_ivar),
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),
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),
361 static driver_t sdhci_acpi_driver = {
364 sizeof(struct sdhci_acpi_softc),
366 static devclass_t sdhci_acpi_devclass;
368 DRIVER_MODULE(sdhci_acpi, acpi, sdhci_acpi_driver, sdhci_acpi_devclass, NULL,
370 MODULE_DEPEND(sdhci_acpi, sdhci, 1, 1, 1);
371 MMC_DECLARE_BRIDGE(sdhci_acpi);