1 /* $NetBSD: fdc_acpi.c,v 1.35 2009/02/17 12:46:01 jmcneill Exp $ */ 2 3 /* 4 * Copyright (c) 2002 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 /* 29 * ACPI attachment for the PC Floppy Controller driver, based on 30 * sys/arch/i386/pnpbios/fdc_pnpbios.c by Jason R. Thorpe 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: fdc_acpi.c,v 1.35 2009/02/17 12:46:01 jmcneill Exp $"); 35 36 #include "rnd.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/callout.h> 41 #include <sys/device.h> 42 #include <sys/buf.h> 43 #include <sys/bufq.h> 44 #include <sys/queue.h> 45 #include <sys/disk.h> 46 #if NRND > 0 47 #include <sys/rnd.h> 48 #endif 49 50 #include <sys/bus.h> 51 #include <sys/intr.h> 52 53 #include <dev/isa/isavar.h> 54 #include <dev/isa/isadmavar.h> 55 56 #include <dev/acpi/acpica.h> 57 #include <dev/acpi/acpireg.h> 58 #include <dev/acpi/acpivar.h> 59 60 #include <dev/isa/fdcvar.h> 61 #include <dev/isa/fdvar.h> 62 #include <dev/isa/fdreg.h> 63 64 #include <dev/acpi/fdc_acpireg.h> 65 66 static int fdc_acpi_match(device_t, cfdata_t, void *); 67 static void fdc_acpi_attach(device_t, device_t, void *); 68 69 struct fdc_acpi_softc { 70 struct fdc_softc sc_fdc; 71 bus_space_handle_t sc_baseioh; 72 struct acpi_devnode *sc_node; /* ACPI devnode */ 73 }; 74 75 static int fdc_acpi_enumerate(struct fdc_acpi_softc *); 76 static void fdc_acpi_getknownfds(struct fdc_acpi_softc *); 77 78 static const struct fd_type *fdc_acpi_nvtotype(const char *, int, int); 79 80 CFATTACH_DECL_NEW(fdc_acpi, sizeof(struct fdc_acpi_softc), fdc_acpi_match, 81 fdc_acpi_attach, NULL, NULL); 82 83 /* 84 * Supported device IDs 85 */ 86 87 static const char * const fdc_acpi_ids[] = { 88 "PNP07??", /* PC standard floppy disk controller */ 89 NULL 90 }; 91 92 /* 93 * fdc_acpi_match: autoconf(9) match routine 94 */ 95 static int 96 fdc_acpi_match(device_t parent, cfdata_t match, void *aux) 97 { 98 struct acpi_attach_args *aa = aux; 99 100 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 101 return 0; 102 103 return acpi_match_hid(aa->aa_node->ad_devinfo, fdc_acpi_ids); 104 } 105 106 /* 107 * fdc_acpi_attach: autoconf(9) attach routine 108 */ 109 static void 110 fdc_acpi_attach(device_t parent, device_t self, void *aux) 111 { 112 struct fdc_acpi_softc *asc = device_private(self); 113 struct fdc_softc *sc = &asc->sc_fdc; 114 struct acpi_attach_args *aa = aux; 115 struct acpi_io *io, *ctlio; 116 struct acpi_irq *irq; 117 struct acpi_drq *drq; 118 struct acpi_resources res; 119 ACPI_STATUS rv; 120 121 sc->sc_dev = self; 122 sc->sc_ic = aa->aa_ic; 123 asc->sc_node = aa->aa_node; 124 125 /* parse resources */ 126 rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS", 127 &res, &acpi_resource_parse_ops_default); 128 if (ACPI_FAILURE(rv)) 129 return; 130 131 /* find our i/o registers */ 132 io = acpi_res_io(&res, 0); 133 if (io == NULL) { 134 aprint_error_dev(sc->sc_dev, 135 "unable to find i/o register resource\n"); 136 goto out; 137 } 138 139 /* find our IRQ */ 140 irq = acpi_res_irq(&res, 0); 141 if (irq == NULL) { 142 aprint_error_dev(sc->sc_dev, "unable to find irq resource\n"); 143 goto out; 144 } 145 146 /* find our DRQ */ 147 drq = acpi_res_drq(&res, 0); 148 if (drq == NULL) { 149 aprint_error_dev(sc->sc_dev, "unable to find drq resource\n"); 150 goto out; 151 } 152 sc->sc_drq = drq->ar_drq; 153 154 sc->sc_iot = aa->aa_iot; 155 if (bus_space_map(sc->sc_iot, io->ar_base, io->ar_length, 156 0, &asc->sc_baseioh)) { 157 aprint_error_dev(sc->sc_dev, "can't map i/o space\n"); 158 goto out; 159 } 160 161 switch (io->ar_length) { 162 case 4: 163 sc->sc_ioh = asc->sc_baseioh; 164 break; 165 case 6: 166 if (bus_space_subregion(sc->sc_iot, asc->sc_baseioh, 2, 4, 167 &sc->sc_ioh)) { 168 aprint_error_dev(sc->sc_dev, 169 "unable to subregion i/o space\n"); 170 goto out; 171 } 172 break; 173 default: 174 aprint_error_dev(sc->sc_dev, 175 "unknown size: %d of io mapping\n", io->ar_length); 176 goto out; 177 } 178 179 /* 180 * omitting the controller I/O port. (One has to exist for there to 181 * be a working fdc). Just try and force the mapping in. 182 */ 183 ctlio = acpi_res_io(&res, 1); 184 if (ctlio == NULL) { 185 if (bus_space_map(sc->sc_iot, io->ar_base + io->ar_length + 1, 186 1, 0, &sc->sc_fdctlioh)) { 187 aprint_error_dev(sc->sc_dev, 188 "unable to force map ctl i/o space\n"); 189 goto out; 190 } 191 aprint_verbose_dev(sc->sc_dev, 192 "ctl io %x did't probe. Forced attach\n", 193 io->ar_base + io->ar_length + 1); 194 } else { 195 if (bus_space_map(sc->sc_iot, ctlio->ar_base, ctlio->ar_length, 196 0, &sc->sc_fdctlioh)) { 197 aprint_error_dev(sc->sc_dev, 198 "unable to map ctl i/o space\n"); 199 goto out; 200 } 201 } 202 203 sc->sc_ih = isa_intr_establish(aa->aa_ic, irq->ar_irq, 204 (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL, 205 IPL_BIO, fdcintr, sc); 206 207 /* Setup direct configuration of floppy drives */ 208 sc->sc_present = fdc_acpi_enumerate(asc); 209 if (sc->sc_present >= 0) { 210 sc->sc_known = 1; 211 fdc_acpi_getknownfds(asc); 212 } else { 213 /* 214 * XXX if there is no _FDE control method, attempt to 215 * probe without pnp 216 */ 217 #ifdef ACPI_FDC_DEBUG 218 aprint_debug_dev(sc->sc_dev, 219 "unable to enumerate, attempting normal probe\n"); 220 #endif 221 } 222 223 fdcattach(sc); 224 225 out: 226 acpi_resource_cleanup(&res); 227 } 228 229 static int 230 fdc_acpi_enumerate(struct fdc_acpi_softc *asc) 231 { 232 struct fdc_softc *sc = &asc->sc_fdc; 233 ACPI_OBJECT *fde; 234 ACPI_BUFFER abuf; 235 ACPI_STATUS rv; 236 UINT32 *p; 237 int i, drives = -1; 238 239 rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDE", &abuf); 240 if (ACPI_FAILURE(rv)) { 241 #ifdef ACPI_FDC_DEBUG 242 aprint_normal_dev(sc->sc_dev, "failed to evaluate _FDE: %s\n", 243 AcpiFormatException(rv)); 244 #endif 245 return drives; 246 } 247 fde = (ACPI_OBJECT *)abuf.Pointer; 248 if (fde->Type != ACPI_TYPE_BUFFER) { 249 aprint_error_dev(sc->sc_dev, "expected BUFFER, got %d\n", 250 fde->Type); 251 goto out; 252 } 253 if (fde->Buffer.Length < 5 * sizeof(UINT32)) { 254 aprint_error_dev(sc->sc_dev, 255 "expected buffer len of %lu, got %d\n", 256 (unsigned long)(5 * sizeof(UINT32)), fde->Buffer.Length); 257 goto out; 258 } 259 260 p = (UINT32 *) fde->Buffer.Pointer; 261 262 /* 263 * Indexes 0 through 3 are each UINT32 booleans. True if a drive 264 * is present. 265 */ 266 drives = 0; 267 for (i = 0; i < 4; i++) { 268 if (p[i]) drives |= (1 << i); 269 #ifdef ACPI_FDC_DEBUG 270 aprint_normal_dev(sc->sc_dev, "drive %d %sattached\n", i, 271 p[i] ? "" : "not "); 272 #endif 273 } 274 275 /* 276 * p[4] reports tape presence. Possible values: 277 * 0 - Unknown if device is present 278 * 1 - Device is present 279 * 2 - Device is never present 280 * >2 - Reserved 281 * 282 * we don't currently use this. 283 */ 284 285 out: 286 AcpiOsFree(abuf.Pointer); 287 return drives; 288 } 289 290 static void 291 fdc_acpi_getknownfds(struct fdc_acpi_softc *asc) 292 { 293 struct fdc_softc *sc = &asc->sc_fdc; 294 ACPI_OBJECT *fdi, *e; 295 ACPI_BUFFER abuf; 296 ACPI_STATUS rv; 297 int i; 298 299 for (i = 0; i < 4; i++) { 300 if ((sc->sc_present & (1 << i)) == 0) 301 continue; 302 rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDI", &abuf); 303 if (ACPI_FAILURE(rv)) { 304 #ifdef ACPI_FDC_DEBUG 305 aprint_normal_dev(sc->sc_dev, 306 "failed to evaluate _FDI: %s on drive %d\n", 307 AcpiFormatException(rv), i); 308 #endif 309 /* XXX if _FDI fails, assume 1.44MB floppy */ 310 sc->sc_knownfds[i] = &fdc_acpi_fdtypes[0]; 311 continue; 312 } 313 fdi = (ACPI_OBJECT *)abuf.Pointer; 314 if (fdi->Type != ACPI_TYPE_PACKAGE) { 315 aprint_error_dev(sc->sc_dev, 316 "expected PACKAGE, got %d\n", fdi->Type); 317 goto out; 318 } 319 e = fdi->Package.Elements; 320 sc->sc_knownfds[i] = fdc_acpi_nvtotype( 321 device_xname(sc->sc_dev), 322 e[1].Integer.Value, e[0].Integer.Value); 323 324 /* if fdc_acpi_nvtotype returns NULL, don't attach drive */ 325 if (!sc->sc_knownfds[i]) 326 sc->sc_present &= ~(1 << i); 327 328 out: 329 AcpiOsFree(abuf.Pointer); 330 } 331 } 332 333 static const struct fd_type * 334 fdc_acpi_nvtotype(const char *fdc, int nvraminfo, int drive) 335 { 336 int type; 337 338 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0; 339 switch (type) { 340 case ACPI_FDC_DISKETTE_NONE: 341 return NULL; 342 case ACPI_FDC_DISKETTE_12M: 343 return &fdc_acpi_fdtypes[1]; 344 case ACPI_FDC_DISKETTE_TYPE5: 345 case ACPI_FDC_DISKETTE_TYPE6: 346 case ACPI_FDC_DISKETTE_144M: 347 return &fdc_acpi_fdtypes[0]; 348 case ACPI_FDC_DISKETTE_360K: 349 return &fdc_acpi_fdtypes[3]; 350 case ACPI_FDC_DISKETTE_720K: 351 return &fdc_acpi_fdtypes[4]; 352 default: 353 #ifdef ACPI_FDC_DEBUG 354 aprint_normal("%s: drive %d: unknown device type 0x%x\n", 355 fdc, drive, type); 356 #endif 357 return NULL; 358 } 359 } 360