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, uint8_t val) 101 { 102 struct sdhci_acpi_softc *sc = device_get_softc(dev); 103 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); 107 } 108 109 static uint16_t 110 sdhci_acpi_read_2(device_t dev, struct sdhci_slot *slot, bus_size_t off) 111 { 112 struct sdhci_acpi_softc *sc = device_get_softc(dev); 113 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); 117 } 118 119 static void 120 sdhci_acpi_write_2(device_t dev, struct sdhci_slot *slot, bus_size_t off, uint16_t val) 121 { 122 struct sdhci_acpi_softc *sc = device_get_softc(dev); 123 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); 127 } 128 129 static uint32_t 130 sdhci_acpi_read_4(device_t dev, struct sdhci_slot *slot, bus_size_t off) 131 { 132 struct sdhci_acpi_softc *sc = device_get_softc(dev); 133 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); 137 } 138 139 static void 140 sdhci_acpi_write_4(device_t dev, struct sdhci_slot *slot, bus_size_t off, uint32_t val) 141 { 142 struct sdhci_acpi_softc *sc = device_get_softc(dev); 143 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); 147 } 148 149 static void 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) 152 { 153 struct sdhci_acpi_softc *sc = device_get_softc(dev); 154 155 bus_read_multi_stream_4(sc->mem_res, off, data, count); 156 } 157 158 static void 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) 161 { 162 struct sdhci_acpi_softc *sc = device_get_softc(dev); 163 164 bus_write_multi_stream_4(sc->mem_res, off, data, count); 165 } 166 167 static const struct sdhci_acpi_device * 168 sdhci_acpi_find_device(device_t dev) 169 { 170 const char *hid; 171 int i, uid; 172 ACPI_HANDLE handle; 173 ACPI_STATUS status; 174 175 hid = ACPI_ID_PROBE(device_get_parent(dev), dev, sdhci_ids); 176 if (hid == NULL) 177 return (NULL); 178 179 handle = acpi_get_handle(dev); 180 status = acpi_GetInteger(handle, "_UID", &uid); 181 if (ACPI_FAILURE(status)) 182 uid = 0; 183 184 for (i = 0; sdhci_acpi_devices[i].hid != NULL; i++) { 185 if (strcmp(sdhci_acpi_devices[i].hid, hid) != 0) 186 continue; 187 if ((sdhci_acpi_devices[i].uid != 0) && 188 (sdhci_acpi_devices[i].uid != uid)) 189 continue; 190 return &sdhci_acpi_devices[i]; 191 } 192 193 return (NULL); 194 } 195 196 static int 197 sdhci_acpi_probe(device_t dev) 198 { 199 const struct sdhci_acpi_device *acpi_dev; 200 201 acpi_dev = sdhci_acpi_find_device(dev); 202 if (acpi_dev == NULL) 203 return (ENXIO); 204 205 device_set_desc(dev, acpi_dev->desc); 206 207 return (BUS_PROBE_DEFAULT); 208 } 209 210 static int 211 sdhci_acpi_attach(device_t dev) 212 { 213 struct sdhci_acpi_softc *sc = device_get_softc(dev); 214 int rid, err; 215 const struct sdhci_acpi_device *acpi_dev; 216 217 acpi_dev = sdhci_acpi_find_device(dev); 218 if (acpi_dev == NULL) 219 return (ENXIO); 220 221 sc->quirks = acpi_dev->quirks; 222 223 /* Allocate IRQ. */ 224 rid = 0; 225 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 226 RF_ACTIVE); 227 if (sc->irq_res == NULL) { 228 device_printf(dev, "can't allocate IRQ\n"); 229 return (ENOMEM); 230 } 231 232 rid = 0; 233 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 234 &rid, RF_ACTIVE); 235 if (sc->mem_res == NULL) { 236 device_printf(dev, "can't allocate memory resource for slot\n"); 237 sdhci_acpi_detach(dev); 238 return (ENOMEM); 239 } 240 241 sc->slot.quirks = sc->quirks; 242 243 err = sdhci_init_slot(dev, &sc->slot, 0); 244 if (err) { 245 device_printf(dev, "failed to init slot\n"); 246 sdhci_acpi_detach(dev); 247 return (err); 248 } 249 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); 253 if (err) { 254 device_printf(dev, "can't setup IRQ\n"); 255 sdhci_acpi_detach(dev); 256 return (err); 257 } 258 259 /* Process cards detection. */ 260 sdhci_start_slot(&sc->slot); 261 262 return (0); 263 } 264 265 static int 266 sdhci_acpi_detach(device_t dev) 267 { 268 struct sdhci_acpi_softc *sc = device_get_softc(dev); 269 270 if (sc->intrhand) 271 bus_teardown_intr(dev, sc->irq_res, sc->intrhand); 272 if (sc->irq_res) 273 bus_release_resource(dev, SYS_RES_IRQ, 274 rman_get_rid(sc->irq_res), sc->irq_res); 275 276 if (sc->mem_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); 280 } 281 282 return (0); 283 } 284 285 static int 286 sdhci_acpi_shutdown(device_t dev) 287 { 288 289 return (0); 290 } 291 292 static int 293 sdhci_acpi_suspend(device_t dev) 294 { 295 struct sdhci_acpi_softc *sc = device_get_softc(dev); 296 int err; 297 298 err = bus_generic_suspend(dev); 299 if (err) 300 return (err); 301 sdhci_generic_suspend(&sc->slot); 302 return (0); 303 } 304 305 static int 306 sdhci_acpi_resume(device_t dev) 307 { 308 struct sdhci_acpi_softc *sc = device_get_softc(dev); 309 int err; 310 311 sdhci_generic_resume(&sc->slot); 312 err = bus_generic_resume(dev); 313 if (err) 314 return (err); 315 return (0); 316 } 317 318 static void 319 sdhci_acpi_intr(void *arg) 320 { 321 struct sdhci_acpi_softc *sc = (struct sdhci_acpi_softc *)arg; 322 323 sdhci_generic_intr(&sc->slot); 324 } 325 326 static device_method_t sdhci_methods[] = { 327 /* device_if */ 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), 334 335 /* Bus interface */ 336 DEVMETHOD(bus_read_ivar, sdhci_generic_read_ivar), 337 DEVMETHOD(bus_write_ivar, sdhci_generic_write_ivar), 338 339 /* mmcbr_if */ 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), 345 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), 355 356 DEVMETHOD_END 357 }; 358 359 static driver_t sdhci_acpi_driver = { 360 "sdhci_acpi", 361 sdhci_methods, 362 sizeof(struct sdhci_acpi_softc), 363 }; 364 static devclass_t sdhci_acpi_devclass; 365 366 DRIVER_MODULE(sdhci_acpi, acpi, sdhci_acpi_driver, sdhci_acpi_devclass, NULL, 367 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); 371