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