Home | History | Annotate | Line # | Download | only in pcmcia
fdc_pcmcia.c revision 1.11
      1 /*	$NetBSD: fdc_pcmcia.c,v 1.11 2004/08/08 23:17:12 mycroft 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.11 2004/08/08 23:17:12 mycroft 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 CFATTACH_DECL(fdc_pcmcia, sizeof(struct fdc_pcmcia_softc),
     73     fdc_pcmcia_probe, fdc_pcmcia_attach, NULL, NULL);
     74 
     75 static void
     76 fdc_conf(fdc)
     77 	struct fdc_softc *fdc;
     78 {
     79 	bus_space_tag_t iot = fdc->sc_iot;
     80 	bus_space_handle_t ioh = fdc->sc_ioh;
     81 	int n;
     82 
     83 	/* Figure out what we have */
     84 	if (out_fdc_cmd(iot, ioh, FDC_CMD_VERSION) == -1 ||
     85 	    (n = fdcresult(fdc, 1)) != 1)
     86 		return;
     87 
     88 	/* Nec765 or equivalent */
     89 	if (FDC_ST0(fdc->sc_status[0]) == FDC_ST0_INVL)
     90 		return;
     91 
     92 #if 0
     93 	/* ns8477 check */
     94 	if (out_fdc_cmd(iot, ioh, FDC_CMD_NSC) == -1 ||
     95 	    (n = fdcresult(fdc, 1)) != 1) {
     96 		printf("NSC command failed\n");
     97 		return;
     98 	}
     99 	else
    100 		printf("Version %x\n", fdc->sc_status[0]);
    101 #endif
    102 
    103 	if (out_fdc_cmd(iot, ioh, FDC_CMD_DUMPREG) == -1 ||
    104 	    (n = fdcresult(fdc, -1)) == -1)
    105 		return;
    106 
    107 	/*
    108          * Expect 10 bytes of status; one means that it did not
    109 	 * understand the command
    110 	 */
    111 	if (n == 1)
    112 		return;
    113 
    114 	/*
    115 	 * Configure controller to use FIFO and 8 bytes of FIFO threshold
    116 	 */
    117 	(void)out_fdc_cmd(iot, ioh, FDC_CMD_CONFIGURE);
    118 	(void)out_fdc(iot, ioh, 0x00);	/* doc says 0 */
    119 	(void)out_fdc(iot, ioh, 8);	/* FIFO is active low. */
    120 	(void)out_fdc(iot, ioh, fdc->sc_status[9]); /* same comp */
    121 	/* No result phase */
    122 
    123 	/* Lock this configuration */
    124 	if (out_fdc_cmd(iot, ioh, FDC_CMD_LOCK(FDC_CMD_FLAGS_LOCK)) == -1 ||
    125 	    fdcresult(fdc, 1) != 1)
    126 		return;
    127 }
    128 
    129 int
    130 fdc_pcmcia_probe(parent, match, aux)
    131 	struct device *parent;
    132 	struct cfdata *match;
    133 	void *aux;
    134 {
    135 	struct pcmcia_attach_args *pa = aux;
    136 	struct pcmcia_card *card = pa->card;
    137 	char *cis[4] = PCMCIA_CIS_YEDATA_EXTERNAL_FDD;
    138 
    139 	/* For this card the manufacturer and product are -1 */
    140 	if (strcmp(cis[0], card->cis1_info[0]) == 0 &&
    141 	    strcmp(cis[1], card->cis1_info[1]) == 0)
    142 		return 1;
    143 
    144 	return 0;
    145 }
    146 
    147 
    148 void
    149 fdc_pcmcia_attach(parent, self, aux)
    150 	struct device *parent, *self;
    151 	void *aux;
    152 {
    153 	struct fdc_pcmcia_softc *psc = (void *)self;
    154 	struct fdc_softc *fdc = &psc->sc_fdc;
    155 	struct pcmcia_attach_args *pa = aux;
    156 	struct pcmcia_config_entry *cfe;
    157 	struct pcmcia_function *pf = pa->pf;
    158 	struct fdc_attach_args fa;
    159 
    160 	psc->sc_pf = pf;
    161 
    162 	SIMPLEQ_FOREACH(cfe, &pf->cfe_head, 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, &psc->sc_pcioh,
    187 	    &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