1a54c9cb1SNate Lawson /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 4a54c9cb1SNate Lawson * Copyright (c) 2004 Nate Lawson (SDG) 5a54c9cb1SNate Lawson * All rights reserved. 6a54c9cb1SNate Lawson * 7a54c9cb1SNate Lawson * Redistribution and use in source and binary forms, with or without 8a54c9cb1SNate Lawson * modification, are permitted provided that the following conditions 9a54c9cb1SNate Lawson * are met: 10a54c9cb1SNate Lawson * 1. Redistributions of source code must retain the above copyright 11a54c9cb1SNate Lawson * notice, this list of conditions and the following disclaimer. 12a54c9cb1SNate Lawson * 2. Redistributions in binary form must reproduce the above copyright 13a54c9cb1SNate Lawson * notice, this list of conditions and the following disclaimer in the 14a54c9cb1SNate Lawson * documentation and/or other materials provided with the distribution. 15a54c9cb1SNate Lawson * 16a54c9cb1SNate Lawson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17a54c9cb1SNate Lawson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18a54c9cb1SNate Lawson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19a54c9cb1SNate Lawson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20a54c9cb1SNate Lawson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21a54c9cb1SNate Lawson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22a54c9cb1SNate Lawson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23a54c9cb1SNate Lawson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24a54c9cb1SNate Lawson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25a54c9cb1SNate Lawson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26a54c9cb1SNate Lawson * SUCH DAMAGE. 27a54c9cb1SNate Lawson */ 28a54c9cb1SNate Lawson 29a54c9cb1SNate Lawson #include <sys/param.h> 30a54c9cb1SNate Lawson #include <sys/kernel.h> 31a54c9cb1SNate Lawson #include <sys/bio.h> 32a54c9cb1SNate Lawson #include <sys/bus.h> 33a54c9cb1SNate Lawson #include <sys/malloc.h> 34a54c9cb1SNate Lawson #include <sys/module.h> 35a54c9cb1SNate Lawson #include <sys/proc.h> 36a54c9cb1SNate Lawson 37129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h> 38129d3046SJung-uk Kim 39a54c9cb1SNate Lawson #include <dev/acpica/acpivar.h> 40a54c9cb1SNate Lawson #include <dev/fdc/fdcvar.h> 41a54c9cb1SNate Lawson 42a54c9cb1SNate Lawson static int fdc_acpi_probe(device_t dev); 43a54c9cb1SNate Lawson static int fdc_acpi_attach(device_t dev); 4420819082SNate Lawson static int fdc_acpi_probe_children(device_t bus, device_t dev, 4520819082SNate Lawson void *fde); 46a54c9cb1SNate Lawson static ACPI_STATUS fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, 47a54c9cb1SNate Lawson int level, void *arg); 48a54c9cb1SNate Lawson 4920819082SNate Lawson /* Maximum number of child devices of a controller (4 floppy + 1 tape.) */ 5020819082SNate Lawson #define ACPI_FDC_MAXDEVS 5 5120819082SNate Lawson 52a3f51d19SNate Lawson /* Standard size of buffer returned by the _FDE method. */ 53a3f51d19SNate Lawson #define ACPI_FDC_FDE_LEN (ACPI_FDC_MAXDEVS * sizeof(uint32_t)) 54a3f51d19SNate Lawson 5520819082SNate Lawson /* 5620819082SNate Lawson * Parameters for the tape drive (5th device). Some BIOS authors use this 5720819082SNate Lawson * for all drives, not just the tape drive (e.g., ASUS K8V). This isn't 5820819082SNate Lawson * grossly incompatible with the spec since it says the first four devices 5920819082SNate Lawson * are simple booleans. 6020819082SNate Lawson */ 6120819082SNate Lawson #define ACPI_FD_UNKNOWN 0 6220819082SNate Lawson #define ACPI_FD_PRESENT 1 6320819082SNate Lawson #define ACPI_FD_NEVER_PRESENT 2 64a54c9cb1SNate Lawson 65a54c9cb1SNate Lawson /* Temporary buf length for evaluating _FDE and _FDI. */ 66a54c9cb1SNate Lawson #define ACPI_FDC_BUFLEN 1024 67a54c9cb1SNate Lawson 68a54c9cb1SNate Lawson /* Context for walking FDC child devices. */ 69a54c9cb1SNate Lawson struct fdc_walk_ctx { 7020819082SNate Lawson uint32_t fd_present[ACPI_FDC_MAXDEVS]; 71a54c9cb1SNate Lawson int index; 72a54c9cb1SNate Lawson device_t acpi_dev; 73a54c9cb1SNate Lawson device_t dev; 74a54c9cb1SNate Lawson }; 75a54c9cb1SNate Lawson 76a54c9cb1SNate Lawson static int 77a54c9cb1SNate Lawson fdc_acpi_probe(device_t dev) 78a54c9cb1SNate Lawson { 79a54c9cb1SNate Lawson device_t bus; 80a54c9cb1SNate Lawson static char *fdc_ids[] = { "PNP0700", "PNP0701", NULL }; 815efca36fSTakanori Watanabe int rv; 82a54c9cb1SNate Lawson 83a54c9cb1SNate Lawson bus = device_get_parent(dev); 845efca36fSTakanori Watanabe rv = ACPI_ID_PROBE(bus, dev, fdc_ids, NULL); 855efca36fSTakanori Watanabe if (rv > 0) 865efca36fSTakanori Watanabe return (rv); 87a54c9cb1SNate Lawson 88a54c9cb1SNate Lawson if (ACPI_SUCCESS(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, NULL))) 89a54c9cb1SNate Lawson device_set_desc(dev, "floppy drive controller (FDE)"); 90a54c9cb1SNate Lawson else 91a54c9cb1SNate Lawson device_set_desc(dev, "floppy drive controller"); 925efca36fSTakanori Watanabe return (rv); 93a54c9cb1SNate Lawson } 94a54c9cb1SNate Lawson 95a54c9cb1SNate Lawson static int 96a54c9cb1SNate Lawson fdc_acpi_attach(device_t dev) 97a54c9cb1SNate Lawson { 98a54c9cb1SNate Lawson struct fdc_data *sc; 99a54c9cb1SNate Lawson ACPI_BUFFER buf; 1004affd626SJung-uk Kim ACPI_OBJECT *obj; 101a54c9cb1SNate Lawson device_t bus; 102871d21ceSJung-uk Kim int error; 103a54c9cb1SNate Lawson 104a54c9cb1SNate Lawson /* Get our softc and use the same accessor as ISA. */ 105a54c9cb1SNate Lawson sc = device_get_softc(dev); 106a54c9cb1SNate Lawson sc->fdc_dev = dev; 107a54c9cb1SNate Lawson 108a54c9cb1SNate Lawson /* Initialize variables and get a temporary buffer for _FDE. */ 109a54c9cb1SNate Lawson error = ENXIO; 110a54c9cb1SNate Lawson buf.Length = ACPI_FDC_BUFLEN; 111a54c9cb1SNate Lawson buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO); 112a54c9cb1SNate Lawson if (buf.Pointer == NULL) 113a54c9cb1SNate Lawson goto out; 114a54c9cb1SNate Lawson 115a54c9cb1SNate Lawson /* Allocate resources the same as the ISA attachment. */ 116a54c9cb1SNate Lawson error = fdc_isa_alloc_resources(dev, sc); 117a54c9cb1SNate Lawson if (error != 0) 118a54c9cb1SNate Lawson goto out; 119a54c9cb1SNate Lawson 120a54c9cb1SNate Lawson /* Call common attach code in fdc(4) first. */ 121a54c9cb1SNate Lawson error = fdc_attach(dev); 122a54c9cb1SNate Lawson if (error != 0) 123a54c9cb1SNate Lawson goto out; 124a54c9cb1SNate Lawson 125a54c9cb1SNate Lawson /* 126a54c9cb1SNate Lawson * Enumerate _FDE, which lists floppy drives that are present. If 127a54c9cb1SNate Lawson * this fails, fall back to the ISA hints-based probe method. 128a54c9cb1SNate Lawson */ 129a54c9cb1SNate Lawson bus = device_get_parent(dev); 130a3f51d19SNate Lawson if (ACPI_FAILURE(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, &buf))) { 131871d21ceSJung-uk Kim error = fdc_hints_probe(dev); 132871d21ceSJung-uk Kim goto out; 13320819082SNate Lawson } 134a3f51d19SNate Lawson 135a3f51d19SNate Lawson /* Add fd child devices as specified. */ 1364affd626SJung-uk Kim obj = buf.Pointer; 1374affd626SJung-uk Kim error = fdc_acpi_probe_children(bus, dev, obj->Buffer.Pointer); 138a3f51d19SNate Lawson 13930b49000SJohn Baldwin out: 140a54c9cb1SNate Lawson if (buf.Pointer) 141a54c9cb1SNate Lawson free(buf.Pointer, M_TEMP); 142a54c9cb1SNate Lawson if (error != 0) 143a54c9cb1SNate Lawson fdc_release_resources(sc); 144e45163b2SJohn Baldwin else 145e45163b2SJohn Baldwin fdc_start_worker(dev); 146a54c9cb1SNate Lawson 147a54c9cb1SNate Lawson return (error); 148a54c9cb1SNate Lawson } 149a54c9cb1SNate Lawson 15020819082SNate Lawson static int 15120819082SNate Lawson fdc_acpi_probe_children(device_t bus, device_t dev, void *fde) 15220819082SNate Lawson { 15320819082SNate Lawson struct fdc_walk_ctx *ctx; 15420819082SNate Lawson devclass_t fd_dc; 15520819082SNate Lawson int i; 15620819082SNate Lawson 15720819082SNate Lawson /* Setup the context and walk all child devices. */ 15820819082SNate Lawson ctx = malloc(sizeof(struct fdc_walk_ctx), M_TEMP, M_NOWAIT); 15920819082SNate Lawson if (ctx == NULL) { 16020819082SNate Lawson device_printf(dev, "no memory for walking children\n"); 16120819082SNate Lawson return (ENOMEM); 16220819082SNate Lawson } 16320819082SNate Lawson bcopy(fde, ctx->fd_present, sizeof(ctx->fd_present)); 16420819082SNate Lawson ctx->index = 0; 16520819082SNate Lawson ctx->dev = dev; 16620819082SNate Lawson ctx->acpi_dev = bus; 16720819082SNate Lawson ACPI_SCAN_CHILDREN(ctx->acpi_dev, dev, 1, fdc_acpi_probe_child, 16820819082SNate Lawson ctx); 16920819082SNate Lawson 17020819082SNate Lawson /* Add any devices not represented by an AML Device handle/node. */ 17120819082SNate Lawson fd_dc = devclass_find("fd"); 17220819082SNate Lawson for (i = 0; i < ACPI_FDC_MAXDEVS; i++) 17320819082SNate Lawson if (ctx->fd_present[i] == ACPI_FD_PRESENT && 17420819082SNate Lawson devclass_get_device(fd_dc, i) == NULL) { 17520819082SNate Lawson if (fdc_add_child(dev, "fd", i) == NULL) 17620819082SNate Lawson device_printf(dev, "fd add failed\n"); 17720819082SNate Lawson } 17820819082SNate Lawson free(ctx, M_TEMP); 17920819082SNate Lawson 18020819082SNate Lawson /* Attach any children found during the probe. */ 181*18250ec6SJohn Baldwin bus_attach_children(dev); 182*18250ec6SJohn Baldwin return (0); 18320819082SNate Lawson } 18420819082SNate Lawson 185a54c9cb1SNate Lawson static ACPI_STATUS 186a54c9cb1SNate Lawson fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, int level, void *arg) 187a54c9cb1SNate Lawson { 188a54c9cb1SNate Lawson struct fdc_walk_ctx *ctx; 189885128efSNate Lawson device_t child, old_child; 190a54c9cb1SNate Lawson ACPI_BUFFER buf; 191a54c9cb1SNate Lawson ACPI_OBJECT *pkg, *obj; 192a54c9cb1SNate Lawson ACPI_STATUS status; 193a54c9cb1SNate Lawson 19420819082SNate Lawson ctx = (struct fdc_walk_ctx *)arg; 19520819082SNate Lawson buf.Pointer = NULL; 19620819082SNate Lawson 197a54c9cb1SNate Lawson /* 198a54c9cb1SNate Lawson * The first four ints are booleans that indicate whether fd0-3 are 199a54c9cb1SNate Lawson * present or not. The last is for a tape device, which we don't 200a54c9cb1SNate Lawson * bother supporting for now. 201a54c9cb1SNate Lawson */ 202a54c9cb1SNate Lawson if (ctx->index > 3) 203a54c9cb1SNate Lawson return (AE_OK); 204a54c9cb1SNate Lawson 20520819082SNate Lawson /* This device is not present, move on to the next. */ 20620819082SNate Lawson if (ctx->fd_present[ctx->index] != ACPI_FD_PRESENT) 20720819082SNate Lawson goto out; 20820819082SNate Lawson 20920819082SNate Lawson /* Create a device for the child with the given index. */ 21020819082SNate Lawson child = fdc_add_child(ctx->dev, "fd", ctx->index); 21120819082SNate Lawson if (child == NULL) 21220819082SNate Lawson goto out; 213885128efSNate Lawson old_child = *dev; 21420819082SNate Lawson *dev = child; 21520819082SNate Lawson 216a54c9cb1SNate Lawson /* Get temporary buffer for _FDI probe. */ 217a54c9cb1SNate Lawson buf.Length = ACPI_FDC_BUFLEN; 218a54c9cb1SNate Lawson buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO); 219a54c9cb1SNate Lawson if (buf.Pointer == NULL) 220a54c9cb1SNate Lawson goto out; 221a54c9cb1SNate Lawson 222885128efSNate Lawson /* 223885128efSNate Lawson * Evaluate _FDI to get drive type to pass to the child. We use the 224885128efSNate Lawson * old child here since it has a valid ACPI_HANDLE since it is a 225885128efSNate Lawson * child of acpi. A better way to implement this would be to make fdc 226885128efSNate Lawson * support the ACPI handle ivar for its children. 227885128efSNate Lawson */ 228885128efSNate Lawson status = ACPI_EVALUATE_OBJECT(ctx->acpi_dev, old_child, "_FDI", NULL, 229885128efSNate Lawson &buf); 230a54c9cb1SNate Lawson if (ACPI_FAILURE(status)) { 23120819082SNate Lawson if (status != AE_NOT_FOUND) 23220819082SNate Lawson device_printf(ctx->dev, "_FDI failed - %#x\n", status); 233a54c9cb1SNate Lawson goto out; 234a54c9cb1SNate Lawson } 235a54c9cb1SNate Lawson pkg = (ACPI_OBJECT *)buf.Pointer; 236a54c9cb1SNate Lawson if (!ACPI_PKG_VALID(pkg, 16)) { 237a54c9cb1SNate Lawson device_printf(ctx->dev, "invalid _FDI package\n"); 238a54c9cb1SNate Lawson goto out; 239a54c9cb1SNate Lawson } 240a54c9cb1SNate Lawson obj = &pkg->Package.Elements[1]; 241a54c9cb1SNate Lawson if (obj == NULL || obj->Type != ACPI_TYPE_INTEGER) { 242a54c9cb1SNate Lawson device_printf(ctx->dev, "invalid type object in _FDI\n"); 243a54c9cb1SNate Lawson goto out; 244a54c9cb1SNate Lawson } 245a54c9cb1SNate Lawson fdc_set_fdtype(child, obj->Integer.Value); 246a54c9cb1SNate Lawson 247a54c9cb1SNate Lawson out: 248a54c9cb1SNate Lawson ctx->index++; 249a54c9cb1SNate Lawson if (buf.Pointer) 250a54c9cb1SNate Lawson free(buf.Pointer, M_TEMP); 251a54c9cb1SNate Lawson return (AE_OK); 252a54c9cb1SNate Lawson } 253a54c9cb1SNate Lawson 254a54c9cb1SNate Lawson static device_method_t fdc_acpi_methods[] = { 255a54c9cb1SNate Lawson /* Device interface */ 256a54c9cb1SNate Lawson DEVMETHOD(device_probe, fdc_acpi_probe), 257a54c9cb1SNate Lawson DEVMETHOD(device_attach, fdc_acpi_attach), 258a54c9cb1SNate Lawson DEVMETHOD(device_detach, fdc_detach), 259a54c9cb1SNate Lawson 260a54c9cb1SNate Lawson /* Bus interface */ 261a54c9cb1SNate Lawson DEVMETHOD(bus_print_child, fdc_print_child), 262a54c9cb1SNate Lawson DEVMETHOD(bus_read_ivar, fdc_read_ivar), 263a54c9cb1SNate Lawson DEVMETHOD(bus_write_ivar, fdc_write_ivar), 264a54c9cb1SNate Lawson 26561bfd867SSofian Brabez DEVMETHOD_END 266a54c9cb1SNate Lawson }; 267a54c9cb1SNate Lawson 268a54c9cb1SNate Lawson static driver_t fdc_acpi_driver = { 269a54c9cb1SNate Lawson "fdc", 270a54c9cb1SNate Lawson fdc_acpi_methods, 271a54c9cb1SNate Lawson sizeof(struct fdc_data) 272a54c9cb1SNate Lawson }; 273a54c9cb1SNate Lawson 2747c146c0cSJohn Baldwin DRIVER_MODULE(fdc, acpi, fdc_acpi_driver, 0, 0); 275