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