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