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>
33 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/resource.h>
39 #include <sys/sysctl.h>
40 #include <sys/taskqueue.h>
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 #include <machine/stdarg.h>
46 #include <contrib/dev/acpica/include/acpi.h>
47 #include <dev/acpica/acpivar.h>
49 #include <dev/mmc/bridge.h>
50 #include <dev/mmc/mmcreg.h>
51 #include <dev/mmc/mmcbrvar.h>
57 static const struct sdhci_acpi_device {
62 } sdhci_acpi_devices[] = {
63 { "80860F14", 1, "Intel Bay Trail eMMC 4.5 Controller",
64 SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE |
65 SDHCI_QUIRK_INTEL_POWER_UP_RESET },
66 { "80860F16", 0, "Intel Bay Trail SD Host Controller",
71 static char *sdhci_ids[] = {
77 struct sdhci_acpi_softc {
78 u_int quirks; /* Chip specific quirks */
79 struct resource *irq_res; /* IRQ resource */
80 void *intrhand; /* Interrupt handle */
82 struct sdhci_slot slot;
83 struct resource *mem_res; /* Memory resource */
86 static void sdhci_acpi_intr(void *arg);
87 static int sdhci_acpi_detach(device_t dev);
90 sdhci_acpi_read_1(device_t dev, struct sdhci_slot *slot, bus_size_t off)
92 struct sdhci_acpi_softc *sc = device_get_softc(dev);
94 bus_barrier(sc->mem_res, 0, 0xFF,
95 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
96 return bus_read_1(sc->mem_res, off);
100 sdhci_acpi_write_1(device_t dev, struct sdhci_slot *slot, bus_size_t off, uint8_t val)
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, uint16_t val)
122 struct sdhci_acpi_softc *sc = device_get_softc(dev);
124 bus_barrier(sc->mem_res, 0, 0xFF,
125 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
126 bus_write_2(sc->mem_res, off, val);
130 sdhci_acpi_read_4(device_t dev, struct sdhci_slot *slot, bus_size_t off)
132 struct sdhci_acpi_softc *sc = device_get_softc(dev);
134 bus_barrier(sc->mem_res, 0, 0xFF,
135 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
136 return bus_read_4(sc->mem_res, off);
140 sdhci_acpi_write_4(device_t dev, struct sdhci_slot *slot, bus_size_t off, uint32_t val)
142 struct sdhci_acpi_softc *sc = device_get_softc(dev);
144 bus_barrier(sc->mem_res, 0, 0xFF,
145 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
146 bus_write_4(sc->mem_res, off, val);
150 sdhci_acpi_read_multi_4(device_t dev, struct sdhci_slot *slot,
151 bus_size_t off, uint32_t *data, bus_size_t count)
153 struct sdhci_acpi_softc *sc = device_get_softc(dev);
155 bus_read_multi_stream_4(sc->mem_res, off, data, count);
159 sdhci_acpi_write_multi_4(device_t dev, struct sdhci_slot *slot,
160 bus_size_t off, uint32_t *data, bus_size_t count)
162 struct sdhci_acpi_softc *sc = device_get_softc(dev);
164 bus_write_multi_stream_4(sc->mem_res, off, data, count);
167 static const struct sdhci_acpi_device *
168 sdhci_acpi_find_device(device_t dev)
175 hid = ACPI_ID_PROBE(device_get_parent(dev), dev, sdhci_ids);
179 handle = acpi_get_handle(dev);
180 status = acpi_GetInteger(handle, "_UID", &uid);
181 if (ACPI_FAILURE(status))
184 for (i = 0; sdhci_acpi_devices[i].hid != NULL; i++) {
185 if (strcmp(sdhci_acpi_devices[i].hid, hid) != 0)
187 if ((sdhci_acpi_devices[i].uid != 0) &&
188 (sdhci_acpi_devices[i].uid != uid))
190 return &sdhci_acpi_devices[i];
197 sdhci_acpi_probe(device_t dev)
199 const struct sdhci_acpi_device *acpi_dev;
201 acpi_dev = sdhci_acpi_find_device(dev);
202 if (acpi_dev == NULL)
205 device_set_desc(dev, acpi_dev->desc);
207 return (BUS_PROBE_DEFAULT);
211 sdhci_acpi_attach(device_t dev)
213 struct sdhci_acpi_softc *sc = device_get_softc(dev);
215 const struct sdhci_acpi_device *acpi_dev;
217 acpi_dev = sdhci_acpi_find_device(dev);
218 if (acpi_dev == NULL)
221 sc->quirks = acpi_dev->quirks;
225 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
227 if (sc->irq_res == NULL) {
228 device_printf(dev, "can't allocate IRQ\n");
233 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
235 if (sc->mem_res == NULL) {
236 device_printf(dev, "can't allocate memory resource for slot\n");
237 sdhci_acpi_detach(dev);
241 sc->slot.quirks = sc->quirks;
243 err = sdhci_init_slot(dev, &sc->slot, 0);
245 device_printf(dev, "failed to init slot\n");
246 sdhci_acpi_detach(dev);
250 /* Activate the interrupt */
251 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
252 NULL, sdhci_acpi_intr, sc, &sc->intrhand);
254 device_printf(dev, "can't setup IRQ\n");
255 sdhci_acpi_detach(dev);
259 /* Process cards detection. */
260 sdhci_start_slot(&sc->slot);
266 sdhci_acpi_detach(device_t dev)
268 struct sdhci_acpi_softc *sc = device_get_softc(dev);
271 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
273 bus_release_resource(dev, SYS_RES_IRQ,
274 rman_get_rid(sc->irq_res), sc->irq_res);
277 sdhci_cleanup_slot(&sc->slot);
278 bus_release_resource(dev, SYS_RES_MEMORY,
279 rman_get_rid(sc->mem_res), sc->mem_res);
286 sdhci_acpi_shutdown(device_t dev)
293 sdhci_acpi_suspend(device_t dev)
295 struct sdhci_acpi_softc *sc = device_get_softc(dev);
298 err = bus_generic_suspend(dev);
301 sdhci_generic_suspend(&sc->slot);
306 sdhci_acpi_resume(device_t dev)
308 struct sdhci_acpi_softc *sc = device_get_softc(dev);
311 sdhci_generic_resume(&sc->slot);
312 err = bus_generic_resume(dev);
319 sdhci_acpi_intr(void *arg)
321 struct sdhci_acpi_softc *sc = (struct sdhci_acpi_softc *)arg;
323 sdhci_generic_intr(&sc->slot);
326 static device_method_t sdhci_methods[] = {
328 DEVMETHOD(device_probe, sdhci_acpi_probe),
329 DEVMETHOD(device_attach, sdhci_acpi_attach),
330 DEVMETHOD(device_detach, sdhci_acpi_detach),
331 DEVMETHOD(device_shutdown, sdhci_acpi_shutdown),
332 DEVMETHOD(device_suspend, sdhci_acpi_suspend),
333 DEVMETHOD(device_resume, sdhci_acpi_resume),
336 DEVMETHOD(bus_read_ivar, sdhci_generic_read_ivar),
337 DEVMETHOD(bus_write_ivar, sdhci_generic_write_ivar),
340 DEVMETHOD(mmcbr_update_ios, sdhci_generic_update_ios),
341 DEVMETHOD(mmcbr_request, sdhci_generic_request),
342 DEVMETHOD(mmcbr_get_ro, sdhci_generic_get_ro),
343 DEVMETHOD(mmcbr_acquire_host, sdhci_generic_acquire_host),
344 DEVMETHOD(mmcbr_release_host, sdhci_generic_release_host),
346 /* SDHCI registers accessors */
347 DEVMETHOD(sdhci_read_1, sdhci_acpi_read_1),
348 DEVMETHOD(sdhci_read_2, sdhci_acpi_read_2),
349 DEVMETHOD(sdhci_read_4, sdhci_acpi_read_4),
350 DEVMETHOD(sdhci_read_multi_4, sdhci_acpi_read_multi_4),
351 DEVMETHOD(sdhci_write_1, sdhci_acpi_write_1),
352 DEVMETHOD(sdhci_write_2, sdhci_acpi_write_2),
353 DEVMETHOD(sdhci_write_4, sdhci_acpi_write_4),
354 DEVMETHOD(sdhci_write_multi_4, sdhci_acpi_write_multi_4),
359 static driver_t sdhci_acpi_driver = {
362 sizeof(struct sdhci_acpi_softc),
364 static devclass_t sdhci_acpi_devclass;
366 DRIVER_MODULE(sdhci_acpi, acpi, sdhci_acpi_driver, sdhci_acpi_devclass, NULL,
368 MODULE_DEPEND(sdhci_acpi, sdhci, 1, 1, 1);
369 DRIVER_MODULE(mmc, sdhci_acpi, mmc_driver, mmc_devclass, NULL, NULL);
370 MODULE_DEPEND(sdhci_acpi, mmc, 1, 1, 1);