Home | History | Annotate | Line # | Download | only in pcmcia
      1  1.23    andvar /*	$NetBSD: fdc_pcmcia.c,v 1.23 2021/12/08 20:50:02 andvar Exp $	*/
      2   1.1  christos 
      3   1.3  christos /*-
      4  1.12   mycroft  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      5   1.3  christos  * All rights reserved.
      6   1.3  christos  *
      7   1.3  christos  * This code is derived from software contributed to The NetBSD Foundation
      8   1.3  christos  * by Christos Zoulas.
      9   1.1  christos  *
     10   1.1  christos  * Redistribution and use in source and binary forms, with or without
     11   1.1  christos  * modification, are permitted provided that the following conditions
     12   1.1  christos  * are met:
     13   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  christos  *    documentation and/or other materials provided with the distribution.
     18   1.1  christos  *
     19   1.3  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.3  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.3  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.3  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.3  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.3  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.3  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.3  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.3  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.3  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.3  christos  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  christos  */
     31   1.5     lukem 
     32   1.5     lukem #include <sys/cdefs.h>
     33  1.23    andvar __KERNEL_RCSID(0, "$NetBSD: fdc_pcmcia.c,v 1.23 2021/12/08 20:50:02 andvar Exp $");
     34   1.1  christos 
     35   1.1  christos #include <sys/param.h>
     36   1.1  christos #include <sys/systm.h>
     37   1.1  christos #include <sys/conf.h>
     38   1.1  christos #include <sys/device.h>
     39   1.2  christos #include <sys/disk.h>
     40   1.2  christos #include <sys/buf.h>
     41   1.1  christos 
     42  1.18        ad #include <sys/bus.h>
     43  1.18        ad #include <sys/intr.h>
     44   1.1  christos 
     45   1.1  christos #include <dev/pcmcia/pcmciareg.h>
     46   1.1  christos #include <dev/pcmcia/pcmciavar.h>
     47   1.2  christos #include <dev/pcmcia/pcmciadevs.h>
     48   1.1  christos 
     49  1.14   mycroft #include <dev/isa/isavar.h>
     50  1.14   mycroft 
     51  1.14   mycroft #include <dev/isa/fdreg.h>
     52  1.14   mycroft #include <dev/isa/fdcvar.h>
     53   1.1  christos 
     54   1.1  christos struct fdc_pcmcia_softc {
     55   1.1  christos 	struct fdc_softc sc_fdc;		/* real "fdc" softc */
     56   1.1  christos 
     57   1.1  christos 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     58   1.1  christos };
     59   1.1  christos 
     60  1.19      cube int fdc_pcmcia_match(device_t, cfdata_t, void *);
     61  1.15     perry int fdc_pcmcia_validate_config(struct pcmcia_config_entry *);
     62  1.19      cube void fdc_pcmcia_attach(device_t, device_t, void *);
     63  1.15     perry static void fdc_conf(struct fdc_softc *);
     64   1.1  christos 
     65  1.19      cube CFATTACH_DECL_NEW(fdc_pcmcia, sizeof(struct fdc_pcmcia_softc),
     66  1.14   mycroft     fdc_pcmcia_match, fdc_pcmcia_attach, NULL, NULL);
     67  1.14   mycroft 
     68  1.14   mycroft const struct pcmcia_product fdc_pcmcia_products[] = {
     69  1.14   mycroft 	{ PCMCIA_VENDOR_YEDATA, PCMCIA_PRODUCT_YEDATA_EXTERNAL_FDD,
     70  1.14   mycroft 	  PCMCIA_CIS_YEDATA_EXTERNAL_FDD },
     71  1.14   mycroft };
     72  1.14   mycroft const size_t fdc_pcmcia_nproducts =
     73  1.14   mycroft     sizeof(fdc_pcmcia_products) / sizeof(fdc_pcmcia_products[0]);
     74   1.1  christos 
     75   1.1  christos static void
     76  1.19      cube fdc_conf(struct fdc_softc *fdc)
     77   1.1  christos {
     78   1.1  christos 	bus_space_tag_t iot = fdc->sc_iot;
     79   1.1  christos 	bus_space_handle_t ioh = fdc->sc_ioh;
     80   1.1  christos 	int n;
     81   1.1  christos 
     82   1.1  christos 	/* Figure out what we have */
     83  1.16     perry 	if (out_fdc_cmd(iot, ioh, FDC_CMD_VERSION) == -1 ||
     84   1.1  christos 	    (n = fdcresult(fdc, 1)) != 1)
     85   1.1  christos 		return;
     86   1.1  christos 
     87   1.1  christos 	/* Nec765 or equivalent */
     88   1.1  christos 	if (FDC_ST0(fdc->sc_status[0]) == FDC_ST0_INVL)
     89   1.1  christos 		return;
     90   1.1  christos 
     91   1.1  christos #if 0
     92   1.1  christos 	/* ns8477 check */
     93  1.16     perry 	if (out_fdc_cmd(iot, ioh, FDC_CMD_NSC) == -1 ||
     94   1.1  christos 	    (n = fdcresult(fdc, 1)) != 1) {
     95   1.1  christos 		printf("NSC command failed\n");
     96   1.1  christos 		return;
     97   1.1  christos 	}
     98   1.1  christos 	else
     99   1.1  christos 		printf("Version %x\n", fdc->sc_status[0]);
    100   1.1  christos #endif
    101   1.1  christos 
    102  1.16     perry 	if (out_fdc_cmd(iot, ioh, FDC_CMD_DUMPREG) == -1 ||
    103   1.1  christos 	    (n = fdcresult(fdc, -1)) == -1)
    104   1.1  christos 		return;
    105   1.1  christos 
    106   1.1  christos 	/*
    107   1.1  christos          * Expect 10 bytes of status; one means that it did not
    108   1.1  christos 	 * understand the command
    109   1.1  christos 	 */
    110   1.1  christos 	if (n == 1)
    111   1.1  christos 		return;
    112   1.1  christos 
    113   1.1  christos 	/*
    114   1.1  christos 	 * Configure controller to use FIFO and 8 bytes of FIFO threshold
    115   1.1  christos 	 */
    116   1.1  christos 	(void)out_fdc_cmd(iot, ioh, FDC_CMD_CONFIGURE);
    117   1.1  christos 	(void)out_fdc(iot, ioh, 0x00);	/* doc says 0 */
    118   1.1  christos 	(void)out_fdc(iot, ioh, 8);	/* FIFO is active low. */
    119   1.1  christos 	(void)out_fdc(iot, ioh, fdc->sc_status[9]); /* same comp */
    120   1.1  christos 	/* No result phase */
    121   1.1  christos 
    122   1.1  christos 	/* Lock this configuration */
    123   1.1  christos 	if (out_fdc_cmd(iot, ioh, FDC_CMD_LOCK(FDC_CMD_FLAGS_LOCK)) == -1 ||
    124   1.1  christos 	    fdcresult(fdc, 1) != 1)
    125   1.1  christos 		return;
    126   1.1  christos }
    127   1.1  christos 
    128   1.1  christos int
    129  1.19      cube fdc_pcmcia_match(device_t parent, cfdata_t match, void *aux)
    130   1.1  christos {
    131   1.1  christos 	struct pcmcia_attach_args *pa = aux;
    132   1.1  christos 
    133  1.14   mycroft 	if (pcmcia_product_lookup(pa, fdc_pcmcia_products, fdc_pcmcia_nproducts,
    134  1.14   mycroft 	    sizeof(fdc_pcmcia_products[0]), NULL))
    135  1.14   mycroft 		return (1);
    136  1.14   mycroft 	return (0);
    137   1.1  christos }
    138   1.1  christos 
    139   1.1  christos void
    140  1.19      cube fdc_pcmcia_attach(device_t parent, device_t self, void *aux)
    141   1.1  christos {
    142  1.19      cube 	struct fdc_pcmcia_softc *psc = device_private(self);
    143   1.1  christos 	struct fdc_softc *fdc = &psc->sc_fdc;
    144   1.1  christos 	struct pcmcia_attach_args *pa = aux;
    145   1.1  christos 	struct pcmcia_config_entry *cfe;
    146   1.1  christos 	struct pcmcia_function *pf = pa->pf;
    147   1.1  christos 	struct fdc_attach_args fa;
    148  1.14   mycroft 	int error;
    149   1.1  christos 
    150  1.19      cube 	fdc->sc_dev = self;
    151   1.1  christos 	psc->sc_pf = pf;
    152   1.1  christos 
    153  1.14   mycroft 	error = pcmcia_function_configure(pf, fdc_pcmcia_validate_config);
    154  1.14   mycroft         if (error) {
    155  1.19      cube                 aprint_error_dev(self, "configure failed, error=%d\n", error);
    156  1.14   mycroft                 return;
    157  1.14   mycroft         }
    158  1.14   mycroft 
    159  1.14   mycroft 	cfe = pf->cfe;
    160  1.14   mycroft 	fdc->sc_iot = cfe->iospace[0].handle.iot;
    161  1.14   mycroft 	fdc->sc_iot = cfe->iospace[0].handle.ioh;
    162   1.1  christos 
    163  1.14   mycroft 	if (pcmcia_function_enable(pf))
    164  1.14   mycroft 		goto fail;
    165   1.2  christos 
    166   1.4  christos 	fdc->sc_flags = FDC_HEADSETTLE;
    167   1.1  christos 	fdc->sc_state = DEVIDLE;
    168   1.1  christos 	TAILQ_INIT(&fdc->sc_drives);
    169   1.1  christos 
    170   1.1  christos 	if (!fdcfind(fdc->sc_iot, fdc->sc_ioh, 1))
    171  1.23    andvar 		aprint_error_dev(self, "couldn't find fdc\n");
    172   1.1  christos 
    173   1.1  christos 	fdc_conf(fdc);
    174   1.1  christos 
    175   1.1  christos 	/* Establish the interrupt handler. */
    176   1.1  christos 	fdc->sc_ih = pcmcia_intr_establish(pa->pf, IPL_BIO, fdchwintr, fdc);
    177  1.14   mycroft 	if (!fdc->sc_ih)
    178  1.14   mycroft 		goto fail;
    179   1.1  christos 
    180   1.1  christos 	/* physical limit: four drives per controller. */
    181   1.1  christos 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    182   1.1  christos 		if (fa.fa_drive < 2)
    183   1.1  christos 			fa.fa_deftype = &fd_types[0];
    184   1.1  christos 		else
    185   1.1  christos 			fa.fa_deftype = NULL;		/* unknown */
    186  1.22   thorpej 		(void)config_found(self, (void *)&fa, fdprint, CFARGS_NONE);
    187   1.1  christos 	}
    188  1.14   mycroft 
    189  1.14   mycroft 	return;
    190  1.14   mycroft 
    191  1.14   mycroft fail:
    192  1.14   mycroft 	pcmcia_function_unconfigure(pf);
    193   1.1  christos }
    194