Home | History | Annotate | Line # | Download | only in pcmcia
fdc_pcmcia.c revision 1.6
      1 /*	$NetBSD: fdc_pcmcia.c,v 1.6 2002/06/01 23:51:00 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 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.6 2002/06/01 23:51:00 lukem 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 <machine/bus.h>
     50 #include <machine/conf.h>
     51 #include <machine/intr.h>
     52 
     53 #include <dev/pcmcia/pcmciareg.h>
     54 #include <dev/pcmcia/pcmciavar.h>
     55 #include <dev/pcmcia/pcmciadevs.h>
     56 
     57 #include <dev/ic/fdcreg.h>
     58 #include <dev/ic/fdcvar.h>
     59 
     60 struct fdc_pcmcia_softc {
     61 	struct fdc_softc sc_fdc;		/* real "fdc" softc */
     62 
     63 	/* PCMCIA-specific goo. */
     64 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     65 	int sc_io_window;			/* our i/o window */
     66 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     67 };
     68 
     69 int fdc_pcmcia_probe __P((struct device *, struct cfdata *, void *));
     70 void fdc_pcmcia_attach __P((struct device *, struct device *, void *));
     71 static void fdc_conf __P((struct fdc_softc *));
     72 
     73 struct cfattach fdc_pcmcia_ca = {
     74 	sizeof(struct fdc_pcmcia_softc), fdc_pcmcia_probe, fdc_pcmcia_attach
     75 };
     76 
     77 static void
     78 fdc_conf(fdc)
     79 	struct fdc_softc *fdc;
     80 {
     81 	bus_space_tag_t iot = fdc->sc_iot;
     82 	bus_space_handle_t ioh = fdc->sc_ioh;
     83 	int n;
     84 
     85 	/* Figure out what we have */
     86 	if (out_fdc_cmd(iot, ioh, FDC_CMD_VERSION) == -1 ||
     87 	    (n = fdcresult(fdc, 1)) != 1)
     88 		return;
     89 
     90 	/* Nec765 or equivalent */
     91 	if (FDC_ST0(fdc->sc_status[0]) == FDC_ST0_INVL)
     92 		return;
     93 
     94 #if 0
     95 	/* ns8477 check */
     96 	if (out_fdc_cmd(iot, ioh, FDC_CMD_NSC) == -1 ||
     97 	    (n = fdcresult(fdc, 1)) != 1) {
     98 		printf("NSC command failed\n");
     99 		return;
    100 	}
    101 	else
    102 		printf("Version %x\n", fdc->sc_status[0]);
    103 #endif
    104 
    105 	if (out_fdc_cmd(iot, ioh, FDC_CMD_DUMPREG) == -1 ||
    106 	    (n = fdcresult(fdc, -1)) == -1)
    107 		return;
    108 
    109 	/*
    110          * Expect 10 bytes of status; one means that it did not
    111 	 * understand the command
    112 	 */
    113 	if (n == 1)
    114 		return;
    115 
    116 	/*
    117 	 * Configure controller to use FIFO and 8 bytes of FIFO threshold
    118 	 */
    119 	(void)out_fdc_cmd(iot, ioh, FDC_CMD_CONFIGURE);
    120 	(void)out_fdc(iot, ioh, 0x00);	/* doc says 0 */
    121 	(void)out_fdc(iot, ioh, 8);	/* FIFO is active low. */
    122 	(void)out_fdc(iot, ioh, fdc->sc_status[9]); /* same comp */
    123 	/* No result phase */
    124 
    125 	/* Lock this configuration */
    126 	if (out_fdc_cmd(iot, ioh, FDC_CMD_LOCK(FDC_CMD_FLAGS_LOCK)) == -1 ||
    127 	    fdcresult(fdc, 1) != 1)
    128 		return;
    129 }
    130 
    131 int
    132 fdc_pcmcia_probe(parent, match, aux)
    133 	struct device *parent;
    134 	struct cfdata *match;
    135 	void *aux;
    136 {
    137 	struct pcmcia_attach_args *pa = aux;
    138 	struct pcmcia_card *card = pa->card;
    139 	char *cis[4] = PCMCIA_CIS_YEDATA_EXTERNAL_FDD;
    140 
    141 	/* For this card the manufacturer and product are -1 */
    142 	if (strcmp(cis[0], card->cis1_info[0]) == 0 &&
    143 	    strcmp(cis[1], card->cis1_info[1]) == 0)
    144 		return 1;
    145 
    146 	return 0;
    147 }
    148 
    149 
    150 void
    151 fdc_pcmcia_attach(parent, self, aux)
    152 	struct device *parent, *self;
    153 	void *aux;
    154 {
    155 	struct fdc_pcmcia_softc *psc = (void *)self;
    156 	struct fdc_softc *fdc = &psc->sc_fdc;
    157 	struct pcmcia_attach_args *pa = aux;
    158 	struct pcmcia_config_entry *cfe;
    159 	struct pcmcia_function *pf = pa->pf;
    160 	struct fdc_attach_args fa;
    161 
    162 	psc->sc_pf = pf;
    163 
    164 	SIMPLEQ_FOREACH(cfe, &pf->cfe_head, cfe_list) {
    165 		if (cfe->num_memspace != 0 ||
    166 		    cfe->num_iospace != 1)
    167 			continue;
    168 
    169 		if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    170 		    cfe->iospace[0].length, cfe->iospace[0].length,
    171 		    &psc->sc_pcioh) == 0)
    172 			break;
    173 	}
    174 
    175 	if (cfe == 0) {
    176 		printf(": can't alloc i/o space\n");
    177 		return;
    178 	}
    179 
    180 	/* Enable the card. */
    181 	pcmcia_function_init(pf, cfe);
    182 	if (pcmcia_function_enable(pf)) {
    183 		printf(": function enable failed\n");
    184 		return;
    185 	}
    186 
    187 	/* Map in the io space */
    188 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
    189 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    190 		printf(": can't map i/o space\n");
    191 		return;
    192 	}
    193 
    194 	fdc->sc_iot = psc->sc_pcioh.iot;
    195 	fdc->sc_ioh = psc->sc_pcioh.ioh;
    196 
    197 	fdc->sc_flags = FDC_HEADSETTLE;
    198 	fdc->sc_state = DEVIDLE;
    199 	TAILQ_INIT(&fdc->sc_drives);
    200 
    201 	if (!fdcfind(fdc->sc_iot, fdc->sc_ioh, 1))
    202 		printf(": coundn't find fdc\n%s", fdc->sc_dev.dv_xname);
    203 
    204 	printf(": %s\n", PCMCIA_STR_YEDATA_EXTERNAL_FDD);
    205 
    206 	fdc_conf(fdc);
    207 
    208 	/* Establish the interrupt handler. */
    209 	fdc->sc_ih = pcmcia_intr_establish(pa->pf, IPL_BIO, fdchwintr, fdc);
    210 	if (fdc->sc_ih == NULL)
    211 		printf("%s: couldn't establish interrupt\n",
    212 		    fdc->sc_dev.dv_xname);
    213 
    214 	/* physical limit: four drives per controller. */
    215 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    216 		if (fa.fa_drive < 2)
    217 			fa.fa_deftype = &fd_types[0];
    218 		else
    219 			fa.fa_deftype = NULL;		/* unknown */
    220 		(void)config_found(self, (void *)&fa, fdprint);
    221 	}
    222 }
    223