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 SDHCI_QUIRK_MMC_DDR52 |
65 SDHCI_QUIRK_CAPS_BIT63_FOR_MMC_HS400 |
66 SDHCI_QUIRK_PRESET_VALUE_BROKEN },
67 { "80860F14", 3, "Intel Bay Trail SDXC Controller",
68 SDHCI_QUIRK_WAIT_WHILE_BUSY |
69 SDHCI_QUIRK_PRESET_VALUE_BROKEN },
70 { "80860F16", 0, "Intel Bay Trail SDXC Controller",
71 SDHCI_QUIRK_WAIT_WHILE_BUSY |
72 SDHCI_QUIRK_PRESET_VALUE_BROKEN },
76 static char *sdhci_ids[] = {
82 struct sdhci_acpi_softc {
83 u_int quirks; /* Chip specific quirks */
84 struct resource *irq_res; /* IRQ resource */
85 void *intrhand; /* Interrupt handle */
87 struct sdhci_slot slot;
88 struct resource *mem_res; /* Memory resource */
91 static void sdhci_acpi_intr(void *arg);
92 static int sdhci_acpi_detach(device_t dev);
95 sdhci_acpi_read_1(device_t dev, struct sdhci_slot *slot __unused,
98 struct sdhci_acpi_softc *sc = device_get_softc(dev);
100 bus_barrier(sc->mem_res, 0, 0xFF,
101 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
102 return bus_read_1(sc->mem_res, off);
106 sdhci_acpi_write_1(device_t dev, struct sdhci_slot *slot __unused,
107 bus_size_t off, uint8_t val)
109 struct sdhci_acpi_softc *sc = device_get_softc(dev);
111 bus_barrier(sc->mem_res, 0, 0xFF,
112 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
113 bus_write_1(sc->mem_res, off, val);
117 sdhci_acpi_read_2(device_t dev, struct sdhci_slot *slot __unused,
120 struct sdhci_acpi_softc *sc = device_get_softc(dev);
122 bus_barrier(sc->mem_res, 0, 0xFF,
123 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
124 return bus_read_2(sc->mem_res, off);
128 sdhci_acpi_write_2(device_t dev, struct sdhci_slot *slot __unused,
129 bus_size_t off, uint16_t val)
131 struct sdhci_acpi_softc *sc = device_get_softc(dev);
133 bus_barrier(sc->mem_res, 0, 0xFF,
134 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
135 bus_write_2(sc->mem_res, off, val);
139 sdhci_acpi_read_4(device_t dev, struct sdhci_slot *slot __unused,
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 return bus_read_4(sc->mem_res, off);
150 sdhci_acpi_write_4(device_t dev, struct sdhci_slot *slot __unused,
151 bus_size_t off, uint32_t val)
153 struct sdhci_acpi_softc *sc = device_get_softc(dev);
155 bus_barrier(sc->mem_res, 0, 0xFF,
156 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
157 bus_write_4(sc->mem_res, off, val);
161 sdhci_acpi_read_multi_4(device_t dev, struct sdhci_slot *slot __unused,
162 bus_size_t off, uint32_t *data, bus_size_t count)
164 struct sdhci_acpi_softc *sc = device_get_softc(dev);
166 bus_read_multi_stream_4(sc->mem_res, off, data, count);
170 sdhci_acpi_write_multi_4(device_t dev, struct sdhci_slot *slot __unused,
171 bus_size_t off, uint32_t *data, bus_size_t count)
173 struct sdhci_acpi_softc *sc = device_get_softc(dev);
175 bus_write_multi_stream_4(sc->mem_res, off, data, count);
178 static const struct sdhci_acpi_device *
179 sdhci_acpi_find_device(device_t dev)
186 hid = ACPI_ID_PROBE(device_get_parent(dev), dev, sdhci_ids);
190 handle = acpi_get_handle(dev);
191 status = acpi_GetInteger(handle, "_UID", &uid);
192 if (ACPI_FAILURE(status))
195 for (i = 0; sdhci_acpi_devices[i].hid != NULL; i++) {
196 if (strcmp(sdhci_acpi_devices[i].hid, hid) != 0)
198 if ((sdhci_acpi_devices[i].uid != 0) &&
199 (sdhci_acpi_devices[i].uid != uid))
201 return (&sdhci_acpi_devices[i]);
208 sdhci_acpi_probe(device_t dev)
210 const struct sdhci_acpi_device *acpi_dev;
212 acpi_dev = sdhci_acpi_find_device(dev);
213 if (acpi_dev == NULL)
216 device_set_desc(dev, acpi_dev->desc);
218 return (BUS_PROBE_DEFAULT);
222 sdhci_acpi_attach(device_t dev)
224 struct sdhci_acpi_softc *sc = device_get_softc(dev);
226 const struct sdhci_acpi_device *acpi_dev;
228 acpi_dev = sdhci_acpi_find_device(dev);
229 if (acpi_dev == NULL)
232 sc->quirks = acpi_dev->quirks;
236 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
238 if (sc->irq_res == NULL) {
239 device_printf(dev, "can't allocate IRQ\n");
244 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
246 if (sc->mem_res == NULL) {
247 device_printf(dev, "can't allocate memory resource for slot\n");
248 sdhci_acpi_detach(dev);
252 sc->quirks &= ~sdhci_quirk_clear;
253 sc->quirks |= sdhci_quirk_set;
254 sc->slot.quirks = sc->quirks;
256 err = sdhci_init_slot(dev, &sc->slot, 0);
258 device_printf(dev, "failed to init slot\n");
259 sdhci_acpi_detach(dev);
263 /* Activate the interrupt */
264 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
265 NULL, sdhci_acpi_intr, sc, &sc->intrhand);
267 device_printf(dev, "can't setup IRQ\n");
268 sdhci_acpi_detach(dev);
272 /* Process cards detection. */
273 sdhci_start_slot(&sc->slot);
279 sdhci_acpi_detach(device_t dev)
281 struct sdhci_acpi_softc *sc = device_get_softc(dev);
284 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
286 bus_release_resource(dev, SYS_RES_IRQ,
287 rman_get_rid(sc->irq_res), sc->irq_res);
290 sdhci_cleanup_slot(&sc->slot);
291 bus_release_resource(dev, SYS_RES_MEMORY,
292 rman_get_rid(sc->mem_res), sc->mem_res);
299 sdhci_acpi_shutdown(device_t dev)
306 sdhci_acpi_suspend(device_t dev)
308 struct sdhci_acpi_softc *sc = device_get_softc(dev);
311 err = bus_generic_suspend(dev);
314 sdhci_generic_suspend(&sc->slot);
319 sdhci_acpi_resume(device_t dev)
321 struct sdhci_acpi_softc *sc = device_get_softc(dev);
324 sdhci_generic_resume(&sc->slot);
325 err = bus_generic_resume(dev);
332 sdhci_acpi_intr(void *arg)
334 struct sdhci_acpi_softc *sc = (struct sdhci_acpi_softc *)arg;
336 sdhci_generic_intr(&sc->slot);
339 static device_method_t sdhci_methods[] = {
341 DEVMETHOD(device_probe, sdhci_acpi_probe),
342 DEVMETHOD(device_attach, sdhci_acpi_attach),
343 DEVMETHOD(device_detach, sdhci_acpi_detach),
344 DEVMETHOD(device_shutdown, sdhci_acpi_shutdown),
345 DEVMETHOD(device_suspend, sdhci_acpi_suspend),
346 DEVMETHOD(device_resume, sdhci_acpi_resume),
349 DEVMETHOD(bus_read_ivar, sdhci_generic_read_ivar),
350 DEVMETHOD(bus_write_ivar, sdhci_generic_write_ivar),
353 DEVMETHOD(mmcbr_update_ios, sdhci_generic_update_ios),
354 DEVMETHOD(mmcbr_switch_vccq, sdhci_generic_switch_vccq),
355 DEVMETHOD(mmcbr_request, sdhci_generic_request),
356 DEVMETHOD(mmcbr_get_ro, sdhci_generic_get_ro),
357 DEVMETHOD(mmcbr_acquire_host, sdhci_generic_acquire_host),
358 DEVMETHOD(mmcbr_release_host, sdhci_generic_release_host),
360 /* SDHCI accessors */
361 DEVMETHOD(sdhci_read_1, sdhci_acpi_read_1),
362 DEVMETHOD(sdhci_read_2, sdhci_acpi_read_2),
363 DEVMETHOD(sdhci_read_4, sdhci_acpi_read_4),
364 DEVMETHOD(sdhci_read_multi_4, sdhci_acpi_read_multi_4),
365 DEVMETHOD(sdhci_write_1, sdhci_acpi_write_1),
366 DEVMETHOD(sdhci_write_2, sdhci_acpi_write_2),
367 DEVMETHOD(sdhci_write_4, sdhci_acpi_write_4),
368 DEVMETHOD(sdhci_write_multi_4, sdhci_acpi_write_multi_4),
369 DEVMETHOD(sdhci_set_uhs_timing, sdhci_generic_set_uhs_timing),
374 static driver_t sdhci_acpi_driver = {
377 sizeof(struct sdhci_acpi_softc),
379 static devclass_t sdhci_acpi_devclass;
381 DRIVER_MODULE(sdhci_acpi, acpi, sdhci_acpi_driver, sdhci_acpi_devclass, NULL,
383 MODULE_DEPEND(sdhci_acpi, sdhci, 1, 1, 1);
384 MMC_DECLARE_BRIDGE(sdhci_acpi);