1/* $NetBSD: fdc_acpi.c,v 1.47 2021/10/15 19:21:45 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.47 2021/10/15 19:21:45 jmcneill Exp $"); 35 36#include <sys/param.h> 37#include <sys/device.h> 38#include <sys/disk.h> 39#include <sys/systm.h> 40 41#include <dev/acpi/acpireg.h> 42#include <dev/acpi/acpivar.h> 43#include <dev/acpi/acpi_intr.h> 44 45#include <dev/isa/isadmavar.h> 46#include <dev/isa/fdcvar.h> 47#include <dev/isa/fdvar.h> 48#include <dev/isa/fdreg.h> 49 50#include <dev/acpi/fdc_acpireg.h> 51 52#define _COMPONENT ACPI_RESOURCE_COMPONENT 53ACPI_MODULE_NAME ("fdc_acpi") 54 55static int fdc_acpi_match(device_t, cfdata_t, void *); 56static void fdc_acpi_attach(device_t, device_t, void *); 57 58struct fdc_acpi_softc { 59 struct fdc_softc sc_fdc; 60 bus_space_handle_t sc_baseioh; 61 struct acpi_devnode *sc_node; /* ACPI devnode */ 62}; 63 64static int fdc_acpi_enumerate(struct fdc_acpi_softc *); 65static void fdc_acpi_getknownfds(struct fdc_acpi_softc *); 66 67static const struct fd_type *fdc_acpi_nvtotype(const char *, int, int); 68 69CFATTACH_DECL_NEW(fdc_acpi, sizeof(struct fdc_acpi_softc), fdc_acpi_match, 70 fdc_acpi_attach, NULL, NULL); 71 72/* 73 * Supported device IDs 74 */ 75 76static const struct device_compatible_entry compat_data[] = { 77 { .compat = "PNP07??" }, /* PC standard floppy disk controller */ 78 DEVICE_COMPAT_EOL 79}; 80 81/* 82 * fdc_acpi_match: autoconf(9) match routine 83 */ 84static int 85fdc_acpi_match(device_t parent, cfdata_t match, void *aux) 86{ 87 struct acpi_attach_args *aa = aux; 88 89 return acpi_compatible_match(aa, compat_data); 90} 91 92/* 93 * fdc_acpi_attach: autoconf(9) attach routine 94 */ 95static void 96fdc_acpi_attach(device_t parent, device_t self, void *aux) 97{ 98 struct fdc_acpi_softc *asc = device_private(self); 99 struct fdc_softc *sc = &asc->sc_fdc; 100 struct acpi_attach_args *aa = aux; 101 struct acpi_io *io, *ctlio; 102 struct acpi_drq *drq; 103 struct acpi_resources res; 104 ACPI_STATUS rv; 105 106 sc->sc_dev = self; 107 sc->sc_ic = aa->aa_ic; 108 asc->sc_node = aa->aa_node; 109 110 /* parse resources */ 111 rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS", 112 &res, &acpi_resource_parse_ops_default); 113 if (ACPI_FAILURE(rv)) 114 return; 115 116 /* find our i/o registers */ 117 io = acpi_res_io(&res, 0); 118 if (io == NULL) { 119 aprint_error_dev(sc->sc_dev, 120 "unable to find i/o register resource\n"); 121 goto out; 122 } 123 124 /* find our DRQ */ 125 drq = acpi_res_drq(&res, 0); 126 if (drq == NULL) { 127 aprint_error_dev(sc->sc_dev, "unable to find drq resource\n"); 128 goto out; 129 } 130 sc->sc_drq = drq->ar_drq; 131 132 sc->sc_iot = aa->aa_iot; 133 if (bus_space_map(sc->sc_iot, io->ar_base, io->ar_length, 134 0, &asc->sc_baseioh)) { 135 aprint_error_dev(sc->sc_dev, "can't map i/o space\n"); 136 goto out; 137 } 138 139 switch (io->ar_length) { 140 case 4: 141 sc->sc_ioh = asc->sc_baseioh; 142 break; 143 case 6: 144 if (bus_space_subregion(sc->sc_iot, asc->sc_baseioh, 2, 4, 145 &sc->sc_ioh)) { 146 aprint_error_dev(sc->sc_dev, 147 "unable to subregion i/o space\n"); 148 goto out; 149 } 150 break; 151 default: 152 aprint_error_dev(sc->sc_dev, 153 "unknown size: %d of io mapping\n", io->ar_length); 154 goto out; 155 } 156 157 /* 158 * omitting the controller I/O port. (One has to exist for there to 159 * be a working fdc). Just try and force the mapping in. 160 */ 161 ctlio = acpi_res_io(&res, 1); 162 if (ctlio == NULL) { 163 if (bus_space_map(sc->sc_iot, io->ar_base + io->ar_length + 1, 164 1, 0, &sc->sc_fdctlioh)) { 165 aprint_error_dev(sc->sc_dev, 166 "unable to force map ctl i/o space\n"); 167 goto out; 168 } 169 aprint_verbose_dev(sc->sc_dev, 170 "ctl io %x did't probe. Forced attach\n", 171 io->ar_base + io->ar_length + 1); 172 } else { 173 if (bus_space_map(sc->sc_iot, ctlio->ar_base, ctlio->ar_length, 174 0, &sc->sc_fdctlioh)) { 175 aprint_error_dev(sc->sc_dev, 176 "unable to map ctl i/o space\n"); 177 goto out; 178 } 179 } 180 181 sc->sc_ih = acpi_intr_establish(self, 182 (uint64_t)(uintptr_t)aa->aa_node->ad_handle, 183 IPL_BIO, false, fdcintr, sc, device_xname(self)); 184 if (sc->sc_ih == NULL) { 185 aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n"); 186 goto out; 187 } 188 189 /* Setup direct configuration of floppy drives */ 190 sc->sc_present = fdc_acpi_enumerate(asc); 191 if (sc->sc_present >= 0) { 192 sc->sc_known = 1; 193 fdc_acpi_getknownfds(asc); 194 } else { 195 /* 196 * XXX if there is no _FDE control method, attempt to 197 * probe without pnp 198 */ 199 aprint_debug_dev(sc->sc_dev, 200 "unable to enumerate, attempting normal probe\n"); 201 } 202 203 fdcattach(sc); 204 205 out: 206 acpi_resource_cleanup(&res); 207} 208 209static int 210fdc_acpi_enumerate(struct fdc_acpi_softc *asc) 211{ 212 struct fdc_softc *sc = &asc->sc_fdc; 213 ACPI_OBJECT *fde; 214 ACPI_BUFFER abuf; 215 ACPI_STATUS rv; 216 uint32_t *p; 217 int i, drives = -1; 218 219 rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDE", &abuf); 220 221 if (ACPI_FAILURE(rv)) { 222 aprint_normal_dev(sc->sc_dev, "failed to evaluate _FDE: %s\n", 223 AcpiFormatException(rv)); 224 return drives; 225 } 226 fde = abuf.Pointer; 227 if (fde->Type != ACPI_TYPE_BUFFER) { 228 aprint_error_dev(sc->sc_dev, "expected BUFFER, got %u\n", 229 fde->Type); 230 goto out; 231 } 232 if (fde->Buffer.Length < 5 * sizeof(uint32_t)) { 233 aprint_error_dev(sc->sc_dev, 234 "expected buffer len of %lu, got %u\n", 235 (unsigned long)(5 * sizeof(uint32_t)), fde->Buffer.Length); 236 goto out; 237 } 238 239 p = (uint32_t *)fde->Buffer.Pointer; 240 241 /* 242 * Indexes 0 through 3 are each uint32_t booleans. True if a drive 243 * is present. 244 */ 245 drives = 0; 246 for (i = 0; i < 4; i++) { 247 if (p[i]) drives |= (1 << i); 248 aprint_debug_dev(sc->sc_dev, "drive %d %sattached\n", i, 249 p[i] ? "" : "not "); 250 } 251 252 /* 253 * p[4] reports tape presence. Possible values: 254 * 0 - Unknown if device is present 255 * 1 - Device is present 256 * 2 - Device is never present 257 * >2 - Reserved 258 * 259 * we don't currently use this. 260 */ 261 262out: 263 ACPI_FREE(abuf.Pointer); 264 return drives; 265} 266 267static void 268fdc_acpi_getknownfds(struct fdc_acpi_softc *asc) 269{ 270 struct fdc_softc *sc = &asc->sc_fdc; 271 ACPI_OBJECT *fdi, *e; 272 ACPI_BUFFER abuf; 273 ACPI_STATUS rv; 274 int i; 275 276 for (i = 0; i < 4; i++) { 277 if ((sc->sc_present & (1 << i)) == 0) 278 continue; 279 rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDI", &abuf); 280 if (ACPI_FAILURE(rv)) { 281 /* if _FDI fails, assume 1.44MB floppy */ 282 sc->sc_knownfds[i] = &fdc_acpi_fdtypes[0]; 283 continue; 284 } 285 fdi = abuf.Pointer; 286 if (fdi->Type != ACPI_TYPE_PACKAGE) { 287 aprint_error_dev(sc->sc_dev, 288 "_FDI: expected PACKAGE, got %u\n", fdi->Type); 289 goto out; 290 } 291 e = fdi->Package.Elements; 292 sc->sc_knownfds[i] = fdc_acpi_nvtotype( 293 device_xname(sc->sc_dev), 294 e[1].Integer.Value, e[0].Integer.Value); 295 296 /* if fdc_acpi_nvtotype returns NULL, don't attach drive */ 297 if (!sc->sc_knownfds[i]) 298 sc->sc_present &= ~(1 << i); 299 300out: 301 ACPI_FREE(abuf.Pointer); 302 } 303} 304 305static const struct fd_type * 306fdc_acpi_nvtotype(const char *fdc, int nvraminfo, int drive) 307{ 308 int type; 309 310 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0; 311 switch (type) { 312 case ACPI_FDC_DISKETTE_NONE: 313 return NULL; 314 case ACPI_FDC_DISKETTE_12M: 315 return &fdc_acpi_fdtypes[1]; 316 case ACPI_FDC_DISKETTE_TYPE5: 317 case ACPI_FDC_DISKETTE_TYPE6: 318 case ACPI_FDC_DISKETTE_144M: 319 return &fdc_acpi_fdtypes[0]; 320 case ACPI_FDC_DISKETTE_360K: 321 return &fdc_acpi_fdtypes[3]; 322 case ACPI_FDC_DISKETTE_720K: 323 return &fdc_acpi_fdtypes[4]; 324 default: 325 aprint_normal("%s: drive %d: unknown device type 0x%x\n", 326 fdc, drive, type); 327 return NULL; 328 } 329} 330