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