xref: /netbsd-src/sys/dev/pcmcia/fdc_pcmcia.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: fdc_pcmcia.c,v 1.17 2005/12/11 12:23:23 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2004 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.17 2005/12/11 12:23:23 christos 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/isa/isavar.h>
57 
58 #include <dev/isa/fdreg.h>
59 #include <dev/isa/fdcvar.h>
60 
61 struct fdc_pcmcia_softc {
62 	struct fdc_softc sc_fdc;		/* real "fdc" softc */
63 
64 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
65 };
66 
67 int fdc_pcmcia_match(struct device *, struct cfdata *, void *);
68 int fdc_pcmcia_validate_config(struct pcmcia_config_entry *);
69 void fdc_pcmcia_attach(struct device *, struct device *, void *);
70 static void fdc_conf(struct fdc_softc *);
71 
72 CFATTACH_DECL(fdc_pcmcia, sizeof(struct fdc_pcmcia_softc),
73     fdc_pcmcia_match, fdc_pcmcia_attach, NULL, NULL);
74 
75 const struct pcmcia_product fdc_pcmcia_products[] = {
76 	{ PCMCIA_VENDOR_YEDATA, PCMCIA_PRODUCT_YEDATA_EXTERNAL_FDD,
77 	  PCMCIA_CIS_YEDATA_EXTERNAL_FDD },
78 };
79 const size_t fdc_pcmcia_nproducts =
80     sizeof(fdc_pcmcia_products) / sizeof(fdc_pcmcia_products[0]);
81 
82 static void
83 fdc_conf(fdc)
84 	struct fdc_softc *fdc;
85 {
86 	bus_space_tag_t iot = fdc->sc_iot;
87 	bus_space_handle_t ioh = fdc->sc_ioh;
88 	int n;
89 
90 	/* Figure out what we have */
91 	if (out_fdc_cmd(iot, ioh, FDC_CMD_VERSION) == -1 ||
92 	    (n = fdcresult(fdc, 1)) != 1)
93 		return;
94 
95 	/* Nec765 or equivalent */
96 	if (FDC_ST0(fdc->sc_status[0]) == FDC_ST0_INVL)
97 		return;
98 
99 #if 0
100 	/* ns8477 check */
101 	if (out_fdc_cmd(iot, ioh, FDC_CMD_NSC) == -1 ||
102 	    (n = fdcresult(fdc, 1)) != 1) {
103 		printf("NSC command failed\n");
104 		return;
105 	}
106 	else
107 		printf("Version %x\n", fdc->sc_status[0]);
108 #endif
109 
110 	if (out_fdc_cmd(iot, ioh, FDC_CMD_DUMPREG) == -1 ||
111 	    (n = fdcresult(fdc, -1)) == -1)
112 		return;
113 
114 	/*
115          * Expect 10 bytes of status; one means that it did not
116 	 * understand the command
117 	 */
118 	if (n == 1)
119 		return;
120 
121 	/*
122 	 * Configure controller to use FIFO and 8 bytes of FIFO threshold
123 	 */
124 	(void)out_fdc_cmd(iot, ioh, FDC_CMD_CONFIGURE);
125 	(void)out_fdc(iot, ioh, 0x00);	/* doc says 0 */
126 	(void)out_fdc(iot, ioh, 8);	/* FIFO is active low. */
127 	(void)out_fdc(iot, ioh, fdc->sc_status[9]); /* same comp */
128 	/* No result phase */
129 
130 	/* Lock this configuration */
131 	if (out_fdc_cmd(iot, ioh, FDC_CMD_LOCK(FDC_CMD_FLAGS_LOCK)) == -1 ||
132 	    fdcresult(fdc, 1) != 1)
133 		return;
134 }
135 
136 int
137 fdc_pcmcia_match(parent, match, aux)
138 	struct device *parent;
139 	struct cfdata *match;
140 	void *aux;
141 {
142 	struct pcmcia_attach_args *pa = aux;
143 
144 	if (pcmcia_product_lookup(pa, fdc_pcmcia_products, fdc_pcmcia_nproducts,
145 	    sizeof(fdc_pcmcia_products[0]), NULL))
146 		return (1);
147 	return (0);
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 	int error;
162 
163 	psc->sc_pf = pf;
164 
165 	error = pcmcia_function_configure(pf, fdc_pcmcia_validate_config);
166         if (error) {
167                 aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
168                     error);
169                 return;
170         }
171 
172 	cfe = pf->cfe;
173 	fdc->sc_iot = cfe->iospace[0].handle.iot;
174 	fdc->sc_iot = cfe->iospace[0].handle.ioh;
175 
176 	if (pcmcia_function_enable(pf))
177 		goto fail;
178 
179 	fdc->sc_flags = FDC_HEADSETTLE;
180 	fdc->sc_state = DEVIDLE;
181 	TAILQ_INIT(&fdc->sc_drives);
182 
183 	if (!fdcfind(fdc->sc_iot, fdc->sc_ioh, 1))
184 		aprint_error("%s: coundn't find fdc\n", self->dv_xname);
185 
186 	fdc_conf(fdc);
187 
188 	/* Establish the interrupt handler. */
189 	fdc->sc_ih = pcmcia_intr_establish(pa->pf, IPL_BIO, fdchwintr, fdc);
190 	if (!fdc->sc_ih)
191 		goto fail;
192 
193 	/* physical limit: four drives per controller. */
194 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
195 		if (fa.fa_drive < 2)
196 			fa.fa_deftype = &fd_types[0];
197 		else
198 			fa.fa_deftype = NULL;		/* unknown */
199 		(void)config_found(self, (void *)&fa, fdprint);
200 	}
201 
202 	return;
203 
204 fail:
205 	pcmcia_function_unconfigure(pf);
206 }
207