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