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