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