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