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 eMMC 4.5 Controller",
61 SDHCI_QUIRK_ALL_SLOTS_NON_REMOVABLE |
62 SDHCI_QUIRK_INTEL_POWER_UP_RESET |
63 SDHCI_QUIRK_WAIT_WHILE_BUSY },
64 { "80860F14", 3, "Intel Bay Trail SDXC Controller",
65 SDHCI_QUIRK_WAIT_WHILE_BUSY },
66 { "80860F16", 0, "Intel Bay Trail SDXC Controller",
67 SDHCI_QUIRK_WAIT_WHILE_BUSY },
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 __unused,
93 struct sdhci_acpi_softc *sc = device_get_softc(dev);
95 bus_barrier(sc->mem_res, 0, 0xFF,
96 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
97 return bus_read_1(sc->mem_res, off);
101 sdhci_acpi_write_1(device_t dev, struct sdhci_slot *slot __unused,
102 bus_size_t off, uint8_t val)
104 struct sdhci_acpi_softc *sc = device_get_softc(dev);
106 bus_barrier(sc->mem_res, 0, 0xFF,
107 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
108 bus_write_1(sc->mem_res, off, val);
112 sdhci_acpi_read_2(device_t dev, struct sdhci_slot *slot __unused,
115 struct sdhci_acpi_softc *sc = device_get_softc(dev);
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);
123 sdhci_acpi_write_2(device_t dev, struct sdhci_slot *slot __unused,
124 bus_size_t off, uint16_t val)
126 struct sdhci_acpi_softc *sc = device_get_softc(dev);
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);
134 sdhci_acpi_read_4(device_t dev, struct sdhci_slot *slot __unused,
137 struct sdhci_acpi_softc *sc = device_get_softc(dev);
139 bus_barrier(sc->mem_res, 0, 0xFF,
140 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
141 return bus_read_4(sc->mem_res, off);
145 sdhci_acpi_write_4(device_t dev, struct sdhci_slot *slot __unused,
146 bus_size_t off, uint32_t val)
148 struct sdhci_acpi_softc *sc = device_get_softc(dev);
150 bus_barrier(sc->mem_res, 0, 0xFF,
151 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
152 bus_write_4(sc->mem_res, off, val);
156 sdhci_acpi_read_multi_4(device_t dev, struct sdhci_slot *slot __unused,
157 bus_size_t off, uint32_t *data, bus_size_t count)
159 struct sdhci_acpi_softc *sc = device_get_softc(dev);
161 bus_read_multi_stream_4(sc->mem_res, off, data, count);
165 sdhci_acpi_write_multi_4(device_t dev, struct sdhci_slot *slot __unused,
166 bus_size_t off, uint32_t *data, bus_size_t count)
168 struct sdhci_acpi_softc *sc = device_get_softc(dev);
170 bus_write_multi_stream_4(sc->mem_res, off, data, count);
173 static const struct sdhci_acpi_device *
174 sdhci_acpi_find_device(device_t dev)
181 hid = ACPI_ID_PROBE(device_get_parent(dev), dev, sdhci_ids);
185 handle = acpi_get_handle(dev);
186 status = acpi_GetInteger(handle, "_UID", &uid);
187 if (ACPI_FAILURE(status))
190 for (i = 0; sdhci_acpi_devices[i].hid != NULL; i++) {
191 if (strcmp(sdhci_acpi_devices[i].hid, hid) != 0)
193 if ((sdhci_acpi_devices[i].uid != 0) &&
194 (sdhci_acpi_devices[i].uid != uid))
196 return (&sdhci_acpi_devices[i]);
203 sdhci_acpi_probe(device_t dev)
205 const struct sdhci_acpi_device *acpi_dev;
207 acpi_dev = sdhci_acpi_find_device(dev);
208 if (acpi_dev == NULL)
211 device_set_desc(dev, acpi_dev->desc);
213 return (BUS_PROBE_DEFAULT);
217 sdhci_acpi_attach(device_t dev)
219 struct sdhci_acpi_softc *sc = device_get_softc(dev);
221 const struct sdhci_acpi_device *acpi_dev;
223 acpi_dev = sdhci_acpi_find_device(dev);
224 if (acpi_dev == NULL)
227 sc->quirks = acpi_dev->quirks;
231 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
233 if (sc->irq_res == NULL) {
234 device_printf(dev, "can't allocate IRQ\n");
239 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
241 if (sc->mem_res == NULL) {
242 device_printf(dev, "can't allocate memory resource for slot\n");
243 sdhci_acpi_detach(dev);
247 sc->slot.quirks = sc->quirks;
249 err = sdhci_init_slot(dev, &sc->slot, 0);
251 device_printf(dev, "failed to init slot\n");
252 sdhci_acpi_detach(dev);
256 /* Activate the interrupt */
257 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
258 NULL, sdhci_acpi_intr, sc, &sc->intrhand);
260 device_printf(dev, "can't setup IRQ\n");
261 sdhci_acpi_detach(dev);
265 /* Process cards detection. */
266 sdhci_start_slot(&sc->slot);
272 sdhci_acpi_detach(device_t dev)
274 struct sdhci_acpi_softc *sc = device_get_softc(dev);
277 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
279 bus_release_resource(dev, SYS_RES_IRQ,
280 rman_get_rid(sc->irq_res), sc->irq_res);
283 sdhci_cleanup_slot(&sc->slot);
284 bus_release_resource(dev, SYS_RES_MEMORY,
285 rman_get_rid(sc->mem_res), sc->mem_res);
292 sdhci_acpi_shutdown(device_t dev)
299 sdhci_acpi_suspend(device_t dev)
301 struct sdhci_acpi_softc *sc = device_get_softc(dev);
304 err = bus_generic_suspend(dev);
307 sdhci_generic_suspend(&sc->slot);
312 sdhci_acpi_resume(device_t dev)
314 struct sdhci_acpi_softc *sc = device_get_softc(dev);
317 sdhci_generic_resume(&sc->slot);
318 err = bus_generic_resume(dev);
325 sdhci_acpi_intr(void *arg)
327 struct sdhci_acpi_softc *sc = (struct sdhci_acpi_softc *)arg;
329 sdhci_generic_intr(&sc->slot);
332 static device_method_t sdhci_methods[] = {
334 DEVMETHOD(device_probe, sdhci_acpi_probe),
335 DEVMETHOD(device_attach, sdhci_acpi_attach),
336 DEVMETHOD(device_detach, sdhci_acpi_detach),
337 DEVMETHOD(device_shutdown, sdhci_acpi_shutdown),
338 DEVMETHOD(device_suspend, sdhci_acpi_suspend),
339 DEVMETHOD(device_resume, sdhci_acpi_resume),
342 DEVMETHOD(bus_read_ivar, sdhci_generic_read_ivar),
343 DEVMETHOD(bus_write_ivar, sdhci_generic_write_ivar),
346 DEVMETHOD(mmcbr_update_ios, sdhci_generic_update_ios),
347 DEVMETHOD(mmcbr_request, sdhci_generic_request),
348 DEVMETHOD(mmcbr_get_ro, sdhci_generic_get_ro),
349 DEVMETHOD(mmcbr_acquire_host, sdhci_generic_acquire_host),
350 DEVMETHOD(mmcbr_release_host, sdhci_generic_release_host),
352 /* SDHCI registers accessors */
353 DEVMETHOD(sdhci_read_1, sdhci_acpi_read_1),
354 DEVMETHOD(sdhci_read_2, sdhci_acpi_read_2),
355 DEVMETHOD(sdhci_read_4, sdhci_acpi_read_4),
356 DEVMETHOD(sdhci_read_multi_4, sdhci_acpi_read_multi_4),
357 DEVMETHOD(sdhci_write_1, sdhci_acpi_write_1),
358 DEVMETHOD(sdhci_write_2, sdhci_acpi_write_2),
359 DEVMETHOD(sdhci_write_4, sdhci_acpi_write_4),
360 DEVMETHOD(sdhci_write_multi_4, sdhci_acpi_write_multi_4),
365 static driver_t sdhci_acpi_driver = {
368 sizeof(struct sdhci_acpi_softc),
370 static devclass_t sdhci_acpi_devclass;
372 DRIVER_MODULE(sdhci_acpi, acpi, sdhci_acpi_driver, sdhci_acpi_devclass, NULL,
374 MODULE_DEPEND(sdhci_acpi, sdhci, 1, 1, 1);
375 MMC_DECLARE_BRIDGE(sdhci_acpi);