xref: /netbsd-src/sys/dev/isa/fdc_isa.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: fdc_isa.c,v 1.17 2008/03/16 00:58:56 cube 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 Charles M. Hannum.
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 /*-
40  * Copyright (c) 1990 The Regents of the University of California.
41  * All rights reserved.
42  *
43  * This code is derived from software contributed to Berkeley by
44  * Don Ahn.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  * 1. Redistributions of source code must retain the above copyright
50  *    notice, this list of conditions and the following disclaimer.
51  * 2. Redistributions in binary form must reproduce the above copyright
52  *    notice, this list of conditions and the following disclaimer in the
53  *    documentation and/or other materials provided with the distribution.
54  * 3. Neither the name of the University nor the names of its contributors
55  *    may be used to endorse or promote products derived from this software
56  *    without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68  * SUCH DAMAGE.
69  *
70  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
71  */
72 
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: fdc_isa.c,v 1.17 2008/03/16 00:58:56 cube Exp $");
75 
76 #include "rnd.h"
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/callout.h>
81 #include <sys/device.h>
82 #include <sys/buf.h>
83 #include <sys/queue.h>
84 #if NRND > 0
85 #include <sys/rnd.h>
86 #endif
87 
88 #include <sys/bus.h>
89 #include <sys/intr.h>
90 
91 #include <dev/isa/isavar.h>
92 #include <dev/isa/isadmavar.h>
93 
94 #include <dev/isa/fdreg.h>
95 #include <dev/isa/fdcvar.h>
96 
97 static int	fdc_isa_probe(device_t, cfdata_t, void *);
98 static void	fdc_isa_attach(device_t, device_t, void *);
99 static int	fdc_isa_detach(device_t, int);
100 
101 struct fdc_isa_softc {
102 	struct fdc_softc sc_fdc;	/* base fdc device */
103 
104 	bus_space_handle_t sc_baseioh;	/* base I/O handle */
105 };
106 
107 CFATTACH_DECL2_NEW(fdc_isa, sizeof(struct fdc_isa_softc),
108     fdc_isa_probe, fdc_isa_attach, fdc_isa_detach, NULL, NULL, fdc_childdet);
109 
110 #ifdef NEWCONFIG
111 void	fdc_isa_forceintr(void *);
112 #endif
113 
114 static int
115 fdc_isa_probe(device_t parent, cfdata_t match, void *aux)
116 {
117 	struct isa_attach_args *ia = aux;
118 	bus_space_tag_t iot;
119 	bus_space_handle_t ioh, ctl_ioh, base_ioh;
120 	int rv, iobase;
121 
122 	iot = ia->ia_iot;
123 	rv = 0;
124 
125 	if (ia->ia_nio < 1)
126 		return (0);
127 	if (ia->ia_nirq < 1)
128 		return (0);
129 	if (ia->ia_ndrq < 1)
130 		return (0);
131 
132 	if (ISA_DIRECT_CONFIG(ia))
133 		return (0);
134 
135 	/* Disallow wildcarded I/O addresses. */
136 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
137 		return (0);
138 
139 	/* Don't allow wildcarded IRQ/DRQ. */
140 	if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
141 		return (0);
142 
143 	if (ia->ia_drq[0].ir_drq == ISA_UNKNOWN_DRQ)
144 		return (0);
145 
146 	/* Map the I/O space. */
147 	iobase = ia->ia_io[0].ir_addr;
148 	if (bus_space_map(iot, iobase, 6 /* FDC_NPORT */, 0, &base_ioh))
149 		return (0);
150 
151 	if (bus_space_subregion(iot, base_ioh, 2, 4, &ioh)) {
152 		bus_space_unmap(iot, base_ioh, 6);
153 		return (0);
154 	}
155 
156 	if (bus_space_map(iot, iobase + fdctl + 2, 1, 0, &ctl_ioh)) {
157 		bus_space_unmap(iot, base_ioh, 6);
158 		return (0);
159 	}
160 
161 	/* Not needed for the rest of the probe. */
162 	bus_space_unmap(iot, ctl_ioh, 1);
163 
164 	/* reset */
165 	bus_space_write_1(iot, ioh, fdout, 0);
166 	delay(100);
167 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
168 
169 	/* see if it can handle a command */
170 	if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0)
171 		goto out;
172 	out_fdc(iot, ioh, 0xdf);
173 	out_fdc(iot, ioh, 2);
174 
175 	rv = 1;
176 	ia->ia_nio = 1;
177 	ia->ia_io[0].ir_size = FDC_NPORT;
178 
179 	ia->ia_nirq = 1;
180 	ia->ia_ndrq = 1;
181 
182 	ia->ia_niomem = 0;
183 
184  out:
185 	bus_space_unmap(iot, base_ioh, 6 /* FDC_NPORT */);
186 	return (rv);
187 }
188 
189 static int
190 fdc_isa_detach(device_t self, int flags)
191 {
192 	int rc;
193 	struct fdc_isa_softc *isc = device_private(self);
194 	struct fdc_softc *fdc = &isc->sc_fdc;
195 
196 	if ((rc = fdcdetach(self, flags)) != 0)
197 		return rc;
198 
199 	isa_intr_disestablish(fdc->sc_ic, fdc->sc_ih);
200 
201 	bus_space_unmap(fdc->sc_iot, fdc->sc_fdctlioh, 1);
202 
203 	bus_space_unmap(fdc->sc_iot, isc->sc_baseioh, 6 /* FDC_NPORT */);
204 
205 	return 0;
206 }
207 
208 static void
209 fdc_isa_attach(device_t parent, device_t self, void *aux)
210 {
211 	struct fdc_isa_softc *isc = device_private(self);
212 	struct fdc_softc *fdc = &isc->sc_fdc;
213 	struct isa_attach_args *ia = aux;
214 
215 	aprint_naive("\n");
216 	aprint_normal("\n");
217 
218 	fdc->sc_dev = self;
219 	fdc->sc_iot = ia->ia_iot;
220 	fdc->sc_ic = ia->ia_ic;
221 	fdc->sc_drq = ia->ia_drq[0].ir_drq;
222 
223 	if (bus_space_map(fdc->sc_iot, ia->ia_io[0].ir_addr,
224 	    6 /* FDC_NPORT */, 0, &isc->sc_baseioh)) {
225 		aprint_normal_dev(fdc->sc_dev, "unable to map I/O space\n");
226 		return;
227 	}
228 
229 	if (bus_space_subregion(fdc->sc_iot, isc->sc_baseioh, 2, 4,
230 	    &fdc->sc_ioh)) {
231 		aprint_normal_dev(fdc->sc_dev,
232 		    "unable to subregion I/O space\n");
233 		return;
234 	}
235 
236 	if (bus_space_map(fdc->sc_iot, ia->ia_io[0].ir_addr + fdctl + 2, 1, 0,
237 	    &fdc->sc_fdctlioh)) {
238 		aprint_normal_dev(fdc->sc_dev,
239 		    "unable to map CTL I/O space\n");
240 		return;
241 	}
242 
243 	fdc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
244 	    IST_EDGE, IPL_BIO, fdcintr, fdc);
245 
246 	fdcattach(fdc);
247 }
248