fdc_acpi.c revision 132810
155682Smarkm/*- 2233294Sstas * Copyright (c) 2004 Nate Lawson (SDG) 3233294Sstas * All rights reserved. 4233294Sstas * 555682Smarkm * Redistribution and use in source and binary forms, with or without 6233294Sstas * modification, are permitted provided that the following conditions 7233294Sstas * are met: 8233294Sstas * 1. Redistributions of source code must retain the above copyright 955682Smarkm * notice, this list of conditions and the following disclaimer. 10233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 11233294Sstas * notice, this list of conditions and the following disclaimer in the 1255682Smarkm * documentation and/or other materials provided with the distribution. 13233294Sstas * 14233294Sstas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1655682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17233294Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2055682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24233294Sstas * SUCH DAMAGE. 25233294Sstas */ 26233294Sstas 27233294Sstas#include <sys/cdefs.h> 28233294Sstas__FBSDID("$FreeBSD: head/sys/dev/fdc/fdc_acpi.c 132810 2004-07-28 22:35:41Z njl $"); 29233294Sstas 30233294Sstas#include <sys/param.h> 31233294Sstas#include <sys/kernel.h> 3255682Smarkm#include <sys/bio.h> 3355682Smarkm#include <sys/bus.h> 3455682Smarkm#include <sys/malloc.h> 3555682Smarkm#include <sys/module.h> 36233294Sstas#include <sys/proc.h> 37233294Sstas 38233294Sstas#include "acpi.h" 39233294Sstas#include <dev/acpica/acpivar.h> 40233294Sstas#include <dev/fdc/fdcreg.h> 41233294Sstas#include <dev/fdc/fdcvar.h> 42233294Sstas 4372445Sassarstatic int fdc_acpi_probe(device_t dev); 44233294Sstasstatic int fdc_acpi_attach(device_t dev); 45178825Sdfrstatic int fdc_acpi_probe_children(device_t bus, device_t dev, 46178825Sdfr void *fde); 47178825Sdfrstatic ACPI_STATUS fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, 48178825Sdfr int level, void *arg); 49178825Sdfrstatic void fdctl_wr_acpi(fdc_p fdc, u_int8_t v); 50178825Sdfr 51233294Sstas/* Maximum number of child devices of a controller (4 floppy + 1 tape.) */ 52233294Sstas#define ACPI_FDC_MAXDEVS 5 53233294Sstas 54233294Sstas/* 55233294Sstas * Parameters for the tape drive (5th device). Some BIOS authors use this 56233294Sstas * for all drives, not just the tape drive (e.g., ASUS K8V). This isn't 57233294Sstas * grossly incompatible with the spec since it says the first four devices 58233294Sstas * are simple booleans. 59233294Sstas */ 60233294Sstas#define ACPI_FD_UNKNOWN 0 6155682Smarkm#define ACPI_FD_PRESENT 1 6255682Smarkm#define ACPI_FD_NEVER_PRESENT 2 6355682Smarkm 6455682Smarkm/* Temporary buf length for evaluating _FDE and _FDI. */ 6555682Smarkm#define ACPI_FDC_BUFLEN 1024 6655682Smarkm 6755682Smarkm/* Context for walking FDC child devices. */ 68178825Sdfrstruct fdc_walk_ctx { 6955682Smarkm uint32_t fd_present[ACPI_FDC_MAXDEVS]; 7055682Smarkm int index; 7155682Smarkm device_t acpi_dev; 72233294Sstas device_t dev; 73233294Sstas}; 74233294Sstas 75233294Sstasstatic void 76233294Sstasfdctl_wr_acpi(fdc_p fdc, u_int8_t v) 77233294Sstas{ 78233294Sstas bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v); 79233294Sstas} 80233294Sstas 81233294Sstasstatic int 82233294Sstasfdc_acpi_probe(device_t dev) 8355682Smarkm{ 8455682Smarkm device_t bus; 8555682Smarkm static char *fdc_ids[] = { "PNP0700", "PNP0701", NULL }; 8655682Smarkm 8755682Smarkm bus = device_get_parent(dev); 8855682Smarkm if (ACPI_ID_PROBE(bus, dev, fdc_ids) == NULL) 8955682Smarkm return (ENXIO); 9055682Smarkm 9155682Smarkm if (ACPI_SUCCESS(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, NULL))) 92233294Sstas device_set_desc(dev, "floppy drive controller (FDE)"); 93233294Sstas else 94233294Sstas device_set_desc(dev, "floppy drive controller"); 95233294Sstas return (0); 96233294Sstas} 97233294Sstas 98233294Sstasstatic int 99233294Sstasfdc_acpi_attach(device_t dev) 100233294Sstas{ 101233294Sstas struct fdc_data *sc; 102233294Sstas ACPI_BUFFER buf; 103233294Sstas device_t bus; 104233294Sstas int error, i, ic_type; 105233294Sstas ACPI_OBJECT *obj, *pkg; 10655682Smarkm ACPI_HANDLE h; 10755682Smarkm uint32_t *fde; 10855682Smarkm 10955682Smarkm /* Get our softc and use the same accessor as ISA. */ 11055682Smarkm sc = device_get_softc(dev); 11155682Smarkm sc->fdc_dev = dev; 11255682Smarkm sc->fdctl_wr = fdctl_wr_acpi; 113233294Sstas sc->flags |= FDC_ISPNP; 114233294Sstas 115233294Sstas /* Initialize variables and get a temporary buffer for _FDE. */ 116233294Sstas error = ENXIO; 117233294Sstas h = acpi_get_handle(dev); 118233294Sstas buf.Length = ACPI_FDC_BUFLEN; 119233294Sstas buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO); 120233294Sstas if (buf.Pointer == NULL) 121233294Sstas goto out; 122233294Sstas 123233294Sstas /* Allocate resources the same as the ISA attachment. */ 124233294Sstas error = fdc_isa_alloc_resources(dev, sc); 125233294Sstas if (error != 0) 126233294Sstas goto out; 127233294Sstas 12855682Smarkm /* Call common attach code in fdc(4) first. */ 12955682Smarkm error = fdc_attach(dev); 13055682Smarkm if (error != 0) 13155682Smarkm goto out; 132233294Sstas 13355682Smarkm /* Check that the controller is working and get its type. */ 13455682Smarkm error = fdc_initial_reset(sc); 135233294Sstas if (error) 136233294Sstas goto out; 137233294Sstas if (fd_cmd(sc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 13878527Sassar ic_type = (u_char)ic_type; 139233294Sstas switch (ic_type) { 14055682Smarkm case 0x80: 14178527Sassar sc->fdct = FDC_NE765; 142233294Sstas break; 143233294Sstas case 0x81: /* not mentioned in any hardware doc */ 144233294Sstas case 0x90: 145233294Sstas sc->fdct = FDC_ENHANCED; 146233294Sstas break; 147233294Sstas default: 14855682Smarkm sc->fdct = FDC_UNKNOWN; 149233294Sstas break; 15055682Smarkm } 151178825Sdfr } 152233294Sstas 153233294Sstas /* 154233294Sstas * Enumerate _FDE, which lists floppy drives that are present. If 155233294Sstas * this fails, fall back to the ISA hints-based probe method. 156233294Sstas */ 157233294Sstas bus = device_get_parent(dev); 158233294Sstas if (ACPI_SUCCESS(ACPI_EVALUATE_OBJECT(bus, dev, "_FDE", NULL, &buf))) { 159178825Sdfr /* 160178825Sdfr * In violation of the spec, systems including the ASUS K8V 161178825Sdfr * return a package of five integers instead of a buffer of 162178825Sdfr * five 32-bit integers. 163178825Sdfr */ 164233294Sstas fde = (uint32_t *)buf.Pointer; 165178825Sdfr pkg = (ACPI_OBJECT *)buf.Pointer; 166233294Sstas if (pkg->Type == ACPI_TYPE_PACKAGE) { 167233294Sstas fde = malloc(pkg->Package.Count * sizeof(uint32_t), 168233294Sstas M_TEMP, M_NOWAIT | M_ZERO); 169233294Sstas if (fde == NULL) { 170233294Sstas error = ENOMEM; 171178825Sdfr goto out; 172178825Sdfr } 173233294Sstas for (i = 0; i < pkg->Package.Count; i++) { 174178825Sdfr obj = &pkg->Package.Elements[i]; 175178825Sdfr if (obj->Type == ACPI_TYPE_INTEGER) 176178825Sdfr fde[i] = (uint32_t)obj->Integer.Value; 177178825Sdfr } 178178825Sdfr } 179178825Sdfr error = fdc_acpi_probe_children(bus, dev, fde); 180178825Sdfr if (pkg->Type == ACPI_TYPE_PACKAGE) 181178825Sdfr free(fde, M_TEMP); 182178825Sdfr } else 183178825Sdfr error = fdc_hints_probe(dev); 184178825Sdfr 185178825Sdfrout: 186178825Sdfr if (buf.Pointer) 187178825Sdfr free(buf.Pointer, M_TEMP); 188178825Sdfr if (error != 0) 189178825Sdfr fdc_release_resources(sc); 190233294Sstas 191233294Sstas return (error); 192233294Sstas} 193233294Sstas 194178825Sdfrstatic int 195178825Sdfrfdc_acpi_probe_children(device_t bus, device_t dev, void *fde) 196178825Sdfr{ 197178825Sdfr struct fdc_walk_ctx *ctx; 198233294Sstas devclass_t fd_dc; 199178825Sdfr int i; 200178825Sdfr 201178825Sdfr /* Setup the context and walk all child devices. */ 202178825Sdfr ctx = malloc(sizeof(struct fdc_walk_ctx), M_TEMP, M_NOWAIT); 203178825Sdfr if (ctx == NULL) { 204178825Sdfr device_printf(dev, "no memory for walking children\n"); 205 return (ENOMEM); 206 } 207 bcopy(fde, ctx->fd_present, sizeof(ctx->fd_present)); 208 ctx->index = 0; 209 ctx->dev = dev; 210 ctx->acpi_dev = bus; 211 ACPI_SCAN_CHILDREN(ctx->acpi_dev, dev, 1, fdc_acpi_probe_child, 212 ctx); 213 214 /* Add any devices not represented by an AML Device handle/node. */ 215 fd_dc = devclass_find("fd"); 216 for (i = 0; i < ACPI_FDC_MAXDEVS; i++) 217 if (ctx->fd_present[i] == ACPI_FD_PRESENT && 218 devclass_get_device(fd_dc, i) == NULL) { 219 if (fdc_add_child(dev, "fd", i) == NULL) 220 device_printf(dev, "fd add failed\n"); 221 } 222 free(ctx, M_TEMP); 223 224 /* Attach any children found during the probe. */ 225 return (bus_generic_attach(dev)); 226} 227 228static ACPI_STATUS 229fdc_acpi_probe_child(ACPI_HANDLE h, device_t *dev, int level, void *arg) 230{ 231 struct fdc_walk_ctx *ctx; 232 device_t child; 233 ACPI_BUFFER buf; 234 ACPI_OBJECT *pkg, *obj; 235 ACPI_STATUS status; 236 237 ctx = (struct fdc_walk_ctx *)arg; 238 buf.Pointer = NULL; 239 240 /* 241 * The first four ints are booleans that indicate whether fd0-3 are 242 * present or not. The last is for a tape device, which we don't 243 * bother supporting for now. 244 */ 245 if (ctx->index > 3) 246 return (AE_OK); 247 248 /* This device is not present, move on to the next. */ 249 if (ctx->fd_present[ctx->index] != ACPI_FD_PRESENT) 250 goto out; 251 252 /* Create a device for the child with the given index. */ 253 child = fdc_add_child(ctx->dev, "fd", ctx->index); 254 if (child == NULL) 255 goto out; 256 *dev = child; 257 258 /* Get temporary buffer for _FDI probe. */ 259 buf.Length = ACPI_FDC_BUFLEN; 260 buf.Pointer = malloc(buf.Length, M_TEMP, M_NOWAIT | M_ZERO); 261 if (buf.Pointer == NULL) 262 goto out; 263 264 /* Evaluate _FDI to get drive type to pass to the child. */ 265 status = ACPI_EVALUATE_OBJECT(ctx->acpi_dev, *dev, "_FDI", NULL, &buf); 266 if (ACPI_FAILURE(status)) { 267 if (status != AE_NOT_FOUND) 268 device_printf(ctx->dev, "_FDI failed - %#x\n", status); 269 goto out; 270 } 271 pkg = (ACPI_OBJECT *)buf.Pointer; 272 if (!ACPI_PKG_VALID(pkg, 16)) { 273 device_printf(ctx->dev, "invalid _FDI package\n"); 274 goto out; 275 } 276 obj = &pkg->Package.Elements[1]; 277 if (obj == NULL || obj->Type != ACPI_TYPE_INTEGER) { 278 device_printf(ctx->dev, "invalid type object in _FDI\n"); 279 goto out; 280 } 281 fdc_set_fdtype(child, obj->Integer.Value); 282 283out: 284 ctx->index++; 285 if (buf.Pointer) 286 free(buf.Pointer, M_TEMP); 287 return (AE_OK); 288} 289 290static device_method_t fdc_acpi_methods[] = { 291 /* Device interface */ 292 DEVMETHOD(device_probe, fdc_acpi_probe), 293 DEVMETHOD(device_attach, fdc_acpi_attach), 294 DEVMETHOD(device_detach, fdc_detach), 295 296 /* Bus interface */ 297 DEVMETHOD(bus_print_child, fdc_print_child), 298 DEVMETHOD(bus_read_ivar, fdc_read_ivar), 299 DEVMETHOD(bus_write_ivar, fdc_write_ivar), 300 301 {0, 0} 302}; 303 304static driver_t fdc_acpi_driver = { 305 "fdc", 306 fdc_acpi_methods, 307 sizeof(struct fdc_data) 308}; 309 310DRIVER_MODULE(fdc, acpi, fdc_acpi_driver, fdc_devclass, 0, 0); 311