Home | History | Annotate | Line # | Download | only in acpi
fdc_acpi.c revision 1.45
      1 /* $NetBSD: fdc_acpi.c,v 1.45 2020/12/07 10:02:51 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.45 2020/12/07 10:02:51 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,
    185 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
    186 	    IPL_BIO, false, fdcintr, sc, device_xname(self));
    187 	if (sc->sc_ih == NULL) {
    188 		aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
    189 		goto out;
    190 	}
    191 
    192 	/* Setup direct configuration of floppy drives */
    193 	sc->sc_present = fdc_acpi_enumerate(asc);
    194 	if (sc->sc_present >= 0) {
    195 		sc->sc_known = 1;
    196 		fdc_acpi_getknownfds(asc);
    197 	} else {
    198 		/*
    199 		 * XXX if there is no _FDE control method, attempt to
    200 		 * probe without pnp
    201 		 */
    202 		aprint_debug_dev(sc->sc_dev,
    203 		    "unable to enumerate, attempting normal probe\n");
    204 	}
    205 
    206 	fdcattach(sc);
    207 
    208  out:
    209 	acpi_resource_cleanup(&res);
    210 }
    211 
    212 static int
    213 fdc_acpi_enumerate(struct fdc_acpi_softc *asc)
    214 {
    215 	struct fdc_softc *sc = &asc->sc_fdc;
    216 	ACPI_OBJECT *fde;
    217 	ACPI_BUFFER abuf;
    218 	ACPI_STATUS rv;
    219 	uint32_t *p;
    220 	int i, drives = -1;
    221 
    222 	rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDE", &abuf);
    223 
    224 	if (ACPI_FAILURE(rv)) {
    225 		aprint_normal_dev(sc->sc_dev, "failed to evaluate _FDE: %s\n",
    226 		    AcpiFormatException(rv));
    227 		return drives;
    228 	}
    229 	fde = abuf.Pointer;
    230 	if (fde->Type != ACPI_TYPE_BUFFER) {
    231 		aprint_error_dev(sc->sc_dev, "expected BUFFER, got %u\n",
    232 		    fde->Type);
    233 		goto out;
    234 	}
    235 	if (fde->Buffer.Length < 5 * sizeof(uint32_t)) {
    236 		aprint_error_dev(sc->sc_dev,
    237 		    "expected buffer len of %lu, got %u\n",
    238 		    (unsigned long)(5 * sizeof(uint32_t)), fde->Buffer.Length);
    239 		goto out;
    240 	}
    241 
    242 	p = (uint32_t *)fde->Buffer.Pointer;
    243 
    244 	/*
    245 	 * Indexes 0 through 3 are each uint32_t booleans. True if a drive
    246 	 * is present.
    247 	 */
    248 	drives = 0;
    249 	for (i = 0; i < 4; i++) {
    250 		if (p[i]) drives |= (1 << i);
    251 		aprint_normal_dev(sc->sc_dev, "drive %d %sattached\n", i,
    252 		    p[i] ? "" : "not ");
    253 	}
    254 
    255 	/*
    256 	 * p[4] reports tape presence. Possible values:
    257 	 * 	0	- Unknown if device is present
    258 	 *	1	- Device is present
    259 	 *	2	- Device is never present
    260 	 *	>2	- Reserved
    261 	 *
    262 	 * we don't currently use this.
    263 	 */
    264 
    265 out:
    266 	ACPI_FREE(abuf.Pointer);
    267 	return drives;
    268 }
    269 
    270 static void
    271 fdc_acpi_getknownfds(struct fdc_acpi_softc *asc)
    272 {
    273 	struct fdc_softc *sc = &asc->sc_fdc;
    274 	ACPI_OBJECT *fdi, *e;
    275 	ACPI_BUFFER abuf;
    276 	ACPI_STATUS rv;
    277 	int i;
    278 
    279 	for (i = 0; i < 4; i++) {
    280 		if ((sc->sc_present & (1 << i)) == 0)
    281 			continue;
    282 		rv = acpi_eval_struct(asc->sc_node->ad_handle, "_FDI", &abuf);
    283 		if (ACPI_FAILURE(rv)) {
    284 			aprint_normal_dev(sc->sc_dev,
    285 			    "failed to evaluate _FDI: %s on drive %d\n",
    286 			    AcpiFormatException(rv), i);
    287 			/* XXX if _FDI fails, assume 1.44MB floppy */
    288 			sc->sc_knownfds[i] = &fdc_acpi_fdtypes[0];
    289 			continue;
    290 		}
    291 		fdi = abuf.Pointer;
    292 		if (fdi->Type != ACPI_TYPE_PACKAGE) {
    293 			aprint_error_dev(sc->sc_dev,
    294 			    "expected PACKAGE, got %u\n", fdi->Type);
    295 			goto out;
    296 		}
    297 		e = fdi->Package.Elements;
    298 		sc->sc_knownfds[i] = fdc_acpi_nvtotype(
    299 		    device_xname(sc->sc_dev),
    300 		    e[1].Integer.Value, e[0].Integer.Value);
    301 
    302 		/* if fdc_acpi_nvtotype returns NULL, don't attach drive */
    303 		if (!sc->sc_knownfds[i])
    304 			sc->sc_present &= ~(1 << i);
    305 
    306 out:
    307 		ACPI_FREE(abuf.Pointer);
    308 	}
    309 }
    310 
    311 static const struct fd_type *
    312 fdc_acpi_nvtotype(const char *fdc, int nvraminfo, int drive)
    313 {
    314 	int type;
    315 
    316 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
    317 	switch (type) {
    318 	case ACPI_FDC_DISKETTE_NONE:
    319 		return NULL;
    320 	case ACPI_FDC_DISKETTE_12M:
    321 		return &fdc_acpi_fdtypes[1];
    322 	case ACPI_FDC_DISKETTE_TYPE5:
    323 	case ACPI_FDC_DISKETTE_TYPE6:
    324 	case ACPI_FDC_DISKETTE_144M:
    325 		return &fdc_acpi_fdtypes[0];
    326 	case ACPI_FDC_DISKETTE_360K:
    327 		return &fdc_acpi_fdtypes[3];
    328 	case ACPI_FDC_DISKETTE_720K:
    329 		return &fdc_acpi_fdtypes[4];
    330 	default:
    331 		aprint_normal("%s: drive %d: unknown device type 0x%x\n",
    332 		    fdc, drive, type);
    333 		return NULL;
    334 	}
    335 }
    336