1 /* $NetBSD: fdc_acpi.c,v 1.18 2003/11/03 19:03:40 mycroft 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.18 2003/11/03 19:03:40 mycroft 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/queue.h> 44 #include <sys/disk.h> 45 #if NRND > 0 46 #include <sys/rnd.h> 47 #endif 48 49 #include <machine/bus.h> 50 #include <machine/intr.h> 51 52 #include <dev/isa/isavar.h> 53 #include <dev/isa/isadmavar.h> 54 55 #include <dev/acpi/acpica.h> 56 #include <dev/acpi/acpireg.h> 57 #include <dev/acpi/acpivar.h> 58 59 #include <dev/isa/fdcvar.h> 60 #include <dev/isa/fdvar.h> 61 #include <dev/isa/fdreg.h> 62 63 #include <dev/acpi/fdc_acpireg.h> 64 65 int fdc_acpi_match(struct device *, struct cfdata *, void *); 66 void fdc_acpi_attach(struct device *, struct device *, void *); 67 68 struct fdc_acpi_softc { 69 struct fdc_softc sc_fdc; 70 bus_space_handle_t sc_baseioh; 71 struct acpi_devnode *sc_node; /* ACPI devnode */ 72 struct acpi_resources res; 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(char *, int, int); 79 80 CFATTACH_DECL(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 int 96 fdc_acpi_match(struct device *parent, struct cfdata *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 void 110 fdc_acpi_attach(struct device *parent, struct device *self, void *aux) 111 { 112 struct fdc_acpi_softc *asc = (struct fdc_acpi_softc *)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 ACPI_STATUS rv; 119 120 printf("\n"); 121 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, &asc->res, 127 &acpi_resource_parse_ops_default); 128 if (ACPI_FAILURE(rv)) 129 return; 130 131 /* find our i/o registers */ 132 io = acpi_res_io(&asc->res, 0); 133 if (io == NULL) { 134 printf("%s: unable to find i/o register resource\n", 135 sc->sc_dev.dv_xname); 136 return; 137 } 138 139 /* find our IRQ */ 140 irq = acpi_res_irq(&asc->res, 0); 141 if (irq == NULL) { 142 printf("%s: unable to find irq resource\n", 143 sc->sc_dev.dv_xname); 144 return; 145 } 146 147 /* find our DRQ */ 148 drq = acpi_res_drq(&asc->res, 0); 149 if (drq == NULL) { 150 printf("%s: unable to find drq resource\n", 151 sc->sc_dev.dv_xname); 152 return; 153 } 154 sc->sc_drq = drq->ar_drq; 155 156 sc->sc_iot = aa->aa_iot; 157 if (bus_space_map(sc->sc_iot, io->ar_base, io->ar_length, 158 0, &asc->sc_baseioh)) { 159 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname); 160 return; 161 } 162 163 switch (io->ar_length) { 164 case 4: 165 sc->sc_ioh = asc->sc_baseioh; 166 break; 167 case 6: 168 if (bus_space_subregion(sc->sc_iot, asc->sc_baseioh, 2, 4, 169 &sc->sc_ioh)) { 170 printf("%s: unable to subregion i/o space\n", 171 sc->sc_dev.dv_xname); 172 return; 173 } 174 break; 175 default: 176 printf("%s: unknown size: %d of io mapping\n", 177 sc->sc_dev.dv_xname, io->ar_length); 178 return; 179 } 180 181 /* 182 * omitting the controller I/O port. (One has to exist for there to 183 * be a working fdc). Just try and force the mapping in. 184 */ 185 ctlio = acpi_res_io(&asc->res, 1); 186 if (ctlio == NULL) { 187 if (bus_space_map(sc->sc_iot, io->ar_base + io->ar_length + 1, 188 1, 0, &sc->sc_fdctlioh)) { 189 printf("%s: unable to force map ctl i/o space\n", 190 sc->sc_dev.dv_xname); 191 return; 192 } 193 printf("%s: ctl io %x did't probe. Forced attach\n", 194 sc->sc_dev.dv_xname, io->ar_base + io->ar_length + 1); 195 } else { 196 if (bus_space_map(sc->sc_iot, ctlio->ar_base, ctlio->ar_length, 197 0, &sc->sc_fdctlioh)) { 198 printf("%s: unable to map ctl i/o space\n", 199 sc->sc_dev.dv_xname); 200 return; 201 } 202 } 203 204 sc->sc_ih = isa_intr_establish(aa->aa_ic, irq->ar_irq, 205 (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL, 206 IPL_BIO, fdcintr, sc); 207 208 /* Setup direct configuration of floppy drives */ 209 sc->sc_present = fdc_acpi_enumerate(asc); 210 if (sc->sc_present >= 0) { 211 sc->sc_known = 1; 212 fdc_acpi_getknownfds(asc); 213 } else { 214 /* 215 * XXX if there is no _FDE control method, attempt to 216 * probe without pnp 217 */ 218 #ifdef ACPI_FDC_DEBUG 219 printf("%s: unable to enumerate, attempting normal probe\n", 220 sc->sc_dev.dv_xname); 221 #endif 222 } 223 224 fdcattach(sc); 225 } 226 227 static int 228 fdc_acpi_enumerate(struct fdc_acpi_softc *asc) 229 { 230 struct fdc_softc *sc = &asc->sc_fdc; 231 ACPI_OBJECT *fde; 232 ACPI_BUFFER buf; 233 ACPI_STATUS rv; 234 UINT32 *p; 235 int i, drives = -1; 236 237 rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDE", &buf); 238 if (ACPI_FAILURE(rv)) { 239 #ifdef ACPI_FDC_DEBUG 240 printf("%s: failed to evaluate _FDE: %s\n", 241 sc->sc_dev.dv_xname, rv, AcpiFormatException(rv)); 242 #endif 243 return drives; 244 } 245 fde = (ACPI_OBJECT *)buf.Pointer; 246 if (fde->Type != ACPI_TYPE_BUFFER) { 247 printf("%s: expected BUFFER, got %d\n", sc->sc_dev.dv_xname, 248 fde->Type); 249 goto out; 250 } 251 if (fde->Buffer.Length < 5 * sizeof(UINT32)) { 252 printf("%s: expected buffer len of %lu, got %d\n", 253 sc->sc_dev.dv_xname, 254 (unsigned long)(5 * sizeof(UINT32)), fde->Buffer.Length); 255 goto out; 256 } 257 258 p = (UINT32 *) fde->Buffer.Pointer; 259 260 /* 261 * Indexes 0 through 3 are each UINT32 booleans. True if a drive 262 * is present. 263 */ 264 drives = 0; 265 for (i = 0; i < 4; i++) { 266 if (p[i]) drives |= (1 << i); 267 #ifdef ACPI_FDC_DEBUG 268 printf("%s: drive %d %sattached\n", sc->sc_dev.dv_xname, i, 269 p[i] ? "" : "not "); 270 #endif 271 } 272 273 /* 274 * p[4] reports tape presence. Possible values: 275 * 0 - Unknown if device is present 276 * 1 - Device is present 277 * 2 - Device is never present 278 * >2 - Reserved 279 * 280 * we don't currently use this. 281 */ 282 283 out: 284 AcpiOsFree(buf.Pointer); 285 return drives; 286 } 287 288 static void 289 fdc_acpi_getknownfds(struct fdc_acpi_softc *asc) 290 { 291 struct fdc_softc *sc = &asc->sc_fdc; 292 ACPI_OBJECT *fdi, *e; 293 ACPI_BUFFER buf; 294 ACPI_STATUS rv; 295 int i; 296 297 for (i = 0; i < 4; i++) { 298 if ((sc->sc_present & (1 << i)) == 0) 299 continue; 300 rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDI", &buf); 301 if (ACPI_FAILURE(rv)) { 302 #ifdef ACPI_FDC_DEBUG 303 printf("%s: failed to evaluate _FDI: %s on drive %d\n", 304 sc->sc_dev.dv_xname, AcpiFormatException(rv), i); 305 #endif 306 /* XXX if _FDI fails, assume 1.44MB floppy */ 307 sc->sc_knownfds[i] = &fdc_acpi_fdtypes[0]; 308 continue; 309 } 310 fdi = (ACPI_OBJECT *)buf.Pointer; 311 if (fdi->Type != ACPI_TYPE_PACKAGE) { 312 printf("%s: expected PACKAGE, got %d\n", 313 sc->sc_dev.dv_xname, fdi->Type); 314 goto out; 315 } 316 e = fdi->Package.Elements; 317 sc->sc_knownfds[i] = fdc_acpi_nvtotype(sc->sc_dev.dv_xname, 318 e[1].Integer.Value, e[0].Integer.Value); 319 320 /* if fdc_acpi_nvtotype returns NULL, don't attach drive */ 321 if (!sc->sc_knownfds[i]) 322 sc->sc_present &= ~(1 << i); 323 324 out: 325 AcpiOsFree(buf.Pointer); 326 } 327 } 328 329 static const struct fd_type * 330 fdc_acpi_nvtotype(char *fdc, int nvraminfo, int drive) 331 { 332 int type; 333 334 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0; 335 switch (type) { 336 case ACPI_FDC_DISKETTE_NONE: 337 return NULL; 338 case ACPI_FDC_DISKETTE_12M: 339 return &fdc_acpi_fdtypes[1]; 340 case ACPI_FDC_DISKETTE_TYPE5: 341 case ACPI_FDC_DISKETTE_TYPE6: 342 case ACPI_FDC_DISKETTE_144M: 343 return &fdc_acpi_fdtypes[0]; 344 case ACPI_FDC_DISKETTE_360K: 345 return &fdc_acpi_fdtypes[3]; 346 case ACPI_FDC_DISKETTE_720K: 347 return &fdc_acpi_fdtypes[4]; 348 default: 349 #ifdef ACPI_FDC_DEBUG 350 printf("%s: drive %d: unknown device type 0x%x\n", 351 fdc, drive, type); 352 #endif 353 return NULL; 354 } 355 } 356