dpt_isa.c revision 1.4.2.2 1 1.4.2.2 bouyer /* $NetBSD: dpt_isa.c,v 1.4.2.2 2000/11/20 11:41:12 bouyer Exp $ */
2 1.4.2.2 bouyer
3 1.4.2.2 bouyer /*
4 1.4.2.2 bouyer * Copyright (c) 1999, 2000 Andrew Doran <ad (at) NetBSD.org>
5 1.4.2.2 bouyer * All rights reserved.
6 1.4.2.2 bouyer *
7 1.4.2.2 bouyer * Redistribution and use in source and binary forms, with or without
8 1.4.2.2 bouyer * modification, are permitted provided that the following conditions
9 1.4.2.2 bouyer * are met:
10 1.4.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer.
12 1.4.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.4.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.4.2.2 bouyer * documentation and/or other materials provided with the distribution.
15 1.4.2.2 bouyer *
16 1.4.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.4.2.2 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.4.2.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.4.2.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.4.2.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.4.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.4.2.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.4.2.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.4.2.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.4.2.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.4.2.2 bouyer * SUCH DAMAGE.
27 1.4.2.2 bouyer *
28 1.4.2.2 bouyer */
29 1.4.2.2 bouyer
30 1.4.2.2 bouyer /*
31 1.4.2.2 bouyer * ISA front-end for DPT EATA SCSI driver.
32 1.4.2.2 bouyer */
33 1.4.2.2 bouyer
34 1.4.2.2 bouyer #include <sys/cdefs.h>
35 1.4.2.2 bouyer __KERNEL_RCSID(0, "$NetBSD: dpt_isa.c,v 1.4.2.2 2000/11/20 11:41:12 bouyer Exp $");
36 1.4.2.2 bouyer
37 1.4.2.2 bouyer #include <sys/types.h>
38 1.4.2.2 bouyer #include <sys/param.h>
39 1.4.2.2 bouyer #include <sys/systm.h>
40 1.4.2.2 bouyer #include <sys/device.h>
41 1.4.2.2 bouyer
42 1.4.2.2 bouyer #include <machine/bus.h>
43 1.4.2.2 bouyer #include <machine/intr.h>
44 1.4.2.2 bouyer
45 1.4.2.2 bouyer #include <dev/scsipi/scsi_all.h>
46 1.4.2.2 bouyer #include <dev/scsipi/scsipi_all.h>
47 1.4.2.2 bouyer #include <dev/scsipi/scsiconf.h>
48 1.4.2.2 bouyer
49 1.4.2.2 bouyer #include <dev/isa/isareg.h>
50 1.4.2.2 bouyer #include <dev/isa/isavar.h>
51 1.4.2.2 bouyer
52 1.4.2.2 bouyer #include <dev/isa/isadmareg.h>
53 1.4.2.2 bouyer #include <dev/isa/isadmavar.h>
54 1.4.2.2 bouyer
55 1.4.2.2 bouyer #include <dev/ic/dptreg.h>
56 1.4.2.2 bouyer #include <dev/ic/dptvar.h>
57 1.4.2.2 bouyer
58 1.4.2.2 bouyer #define DPT_ISA_IOSIZE 16
59 1.4.2.2 bouyer #define DPT_ISA_MAXCCBS 16
60 1.4.2.2 bouyer
61 1.4.2.2 bouyer static int dpt_isa_match __P((struct device *, struct cfdata *, void *));
62 1.4.2.2 bouyer static void dpt_isa_attach __P((struct device *, struct device *, void *));
63 1.4.2.2 bouyer static int dpt_isa_probe __P((struct isa_attach_args *, int));
64 1.4.2.2 bouyer static int dpt_isa_wait __P((bus_space_handle_t, bus_space_tag_t, u_int8_t,
65 1.4.2.2 bouyer u_int8_t));
66 1.4.2.2 bouyer
67 1.4.2.2 bouyer struct cfattach dpt_isa_ca = {
68 1.4.2.2 bouyer sizeof(struct dpt_softc), dpt_isa_match, dpt_isa_attach
69 1.4.2.2 bouyer };
70 1.4.2.2 bouyer
71 1.4.2.2 bouyer /* Try 'less intrusive' addresses first */
72 1.4.2.2 bouyer static int dpt_isa_iobases[] = { 0x230, 0x330, 0x1f0, 0x170, -1 };
73 1.4.2.2 bouyer
74 1.4.2.2 bouyer /*
75 1.4.2.2 bouyer * Wait for the HBA status register to reach a specific state.
76 1.4.2.2 bouyer */
77 1.4.2.2 bouyer static int
78 1.4.2.2 bouyer dpt_isa_wait(ioh, iot, mask, state)
79 1.4.2.2 bouyer bus_space_handle_t ioh;
80 1.4.2.2 bouyer bus_space_tag_t iot;
81 1.4.2.2 bouyer u_int8_t mask, state;
82 1.4.2.2 bouyer {
83 1.4.2.2 bouyer int ms;
84 1.4.2.2 bouyer
85 1.4.2.2 bouyer for (ms = 2000 * 10; ms; ms--) {
86 1.4.2.2 bouyer if ((bus_space_read_1(iot, ioh, HA_STATUS) & mask) == state)
87 1.4.2.2 bouyer return (0);
88 1.4.2.2 bouyer DELAY(100);
89 1.4.2.2 bouyer }
90 1.4.2.2 bouyer return (-1);
91 1.4.2.2 bouyer }
92 1.4.2.2 bouyer
93 1.4.2.2 bouyer /*
94 1.4.2.2 bouyer * Match a supported board.
95 1.4.2.2 bouyer */
96 1.4.2.2 bouyer static int
97 1.4.2.2 bouyer dpt_isa_match(parent, match, aux)
98 1.4.2.2 bouyer struct device *parent;
99 1.4.2.2 bouyer struct cfdata *match;
100 1.4.2.2 bouyer void *aux;
101 1.4.2.2 bouyer {
102 1.4.2.2 bouyer struct isa_attach_args *ia;
103 1.4.2.2 bouyer int i;
104 1.4.2.2 bouyer
105 1.4.2.2 bouyer ia = aux;
106 1.4.2.2 bouyer
107 1.4.2.2 bouyer if (ia->ia_iobase != ISACF_PORT_DEFAULT)
108 1.4.2.2 bouyer return (dpt_isa_probe(ia, ia->ia_iobase));
109 1.4.2.2 bouyer
110 1.4.2.2 bouyer for (i = 0; dpt_isa_iobases[i] != -1; i++) {
111 1.4.2.2 bouyer if (dpt_isa_probe(ia, dpt_isa_iobases[i])) {
112 1.4.2.2 bouyer ia->ia_iobase = dpt_isa_iobases[i];
113 1.4.2.2 bouyer return (1);
114 1.4.2.2 bouyer }
115 1.4.2.2 bouyer }
116 1.4.2.2 bouyer
117 1.4.2.2 bouyer return (0);
118 1.4.2.2 bouyer }
119 1.4.2.2 bouyer
120 1.4.2.2 bouyer /*
121 1.4.2.2 bouyer * Probe for a supported board.
122 1.4.2.2 bouyer */
123 1.4.2.2 bouyer static int
124 1.4.2.2 bouyer dpt_isa_probe(ia, iobase)
125 1.4.2.2 bouyer struct isa_attach_args *ia;
126 1.4.2.2 bouyer int iobase;
127 1.4.2.2 bouyer {
128 1.4.2.2 bouyer struct eata_cfg ec;
129 1.4.2.2 bouyer bus_space_handle_t ioh;
130 1.4.2.2 bouyer bus_space_tag_t iot;
131 1.4.2.2 bouyer int i, j, stat;
132 1.4.2.2 bouyer u_int16_t *p;
133 1.4.2.2 bouyer
134 1.4.2.2 bouyer iot = ia->ia_iot;
135 1.4.2.2 bouyer
136 1.4.2.2 bouyer if (bus_space_map(iot, iobase, DPT_ISA_IOSIZE, 0, &ioh) != 0)
137 1.4.2.2 bouyer return(0);
138 1.4.2.2 bouyer
139 1.4.2.2 bouyer /*
140 1.4.2.2 bouyer * Assumuing the DPT BIOS reset the board, we shouldn't need to
141 1.4.2.2 bouyer * re-do it here. The tests below should weed out non-EATA devices
142 1.4.2.2 bouyer * before we start poking any registers.
143 1.4.2.2 bouyer */
144 1.4.2.2 bouyer for (i = 1000; i; i--) {
145 1.4.2.2 bouyer if ((bus_space_read_1(iot, ioh, HA_STATUS) & HA_ST_READY) != 0)
146 1.4.2.2 bouyer break;
147 1.4.2.2 bouyer DELAY(2000);
148 1.4.2.2 bouyer }
149 1.4.2.2 bouyer
150 1.4.2.2 bouyer if (i == 0) {
151 1.4.2.2 bouyer bus_space_unmap(iot, ioh, DPT_ISA_IOSIZE);
152 1.4.2.2 bouyer return (0);
153 1.4.2.2 bouyer }
154 1.4.2.2 bouyer
155 1.4.2.2 bouyer while((((stat = bus_space_read_1(iot, ioh, HA_STATUS))
156 1.4.2.2 bouyer != (HA_ST_READY|HA_ST_SEEK_COMPLETE))
157 1.4.2.2 bouyer && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR))
158 1.4.2.2 bouyer && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR|HA_ST_DRQ)))
159 1.4.2.2 bouyer || (dpt_isa_wait(ioh, iot, HA_ST_BUSY, 0))) {
160 1.4.2.2 bouyer /* RAID drives still spinning up? */
161 1.4.2.2 bouyer if((bus_space_read_1(iot, ioh, HA_ERROR) != 'D')
162 1.4.2.2 bouyer || (bus_space_read_1(iot, ioh, HA_ERROR + 1) != 'P')
163 1.4.2.2 bouyer || (bus_space_read_1(iot, ioh, HA_ERROR + 2) != 'T')) {
164 1.4.2.2 bouyer bus_space_unmap(iot, ioh, DPT_ISA_IOSIZE);
165 1.4.2.2 bouyer return (0);
166 1.4.2.2 bouyer }
167 1.4.2.2 bouyer }
168 1.4.2.2 bouyer
169 1.4.2.2 bouyer /*
170 1.4.2.2 bouyer * At this point we can be confident that we are dealing with a DPT
171 1.4.2.2 bouyer * HBA. Issue the read-config command and wait for the data to
172 1.4.2.2 bouyer * appear. XXX we shouldn't be doing this with PIO, but it makes it
173 1.4.2.2 bouyer * a lot easier as no DMA setup is required.
174 1.4.2.2 bouyer */
175 1.4.2.2 bouyer bus_space_write_1(iot, ioh, HA_COMMAND, CP_PIO_GETCFG);
176 1.4.2.2 bouyer memset(&ec, 0, sizeof(ec));
177 1.4.2.2 bouyer i = ((int)&((struct eata_cfg *)0)->ec_cfglen +
178 1.4.2.2 bouyer sizeof(ec.ec_cfglen)) >> 1;
179 1.4.2.2 bouyer p = (u_int16_t *)&ec;
180 1.4.2.2 bouyer
181 1.4.2.2 bouyer if (dpt_isa_wait(ioh, iot, 0xFF, HA_ST_DATA_RDY)) {
182 1.4.2.2 bouyer bus_space_unmap(iot, ioh, DPT_ISA_IOSIZE);
183 1.4.2.2 bouyer return (0);
184 1.4.2.2 bouyer }
185 1.4.2.2 bouyer
186 1.4.2.2 bouyer /* Begin reading */
187 1.4.2.2 bouyer while (i--)
188 1.4.2.2 bouyer *p++ = bus_space_read_2(iot, ioh, HA_DATA);
189 1.4.2.2 bouyer
190 1.4.2.2 bouyer if ((i = ec.ec_cfglen) > (sizeof(struct eata_cfg)
191 1.4.2.2 bouyer - (int)(&(((struct eata_cfg *)0L)->ec_cfglen))
192 1.4.2.2 bouyer - sizeof(ec.ec_cfglen)))
193 1.4.2.2 bouyer i = sizeof(struct eata_cfg)
194 1.4.2.2 bouyer - (int)(&(((struct eata_cfg *)0L)->ec_cfglen))
195 1.4.2.2 bouyer - sizeof(ec.ec_cfglen);
196 1.4.2.2 bouyer
197 1.4.2.2 bouyer j = i + (int)(&(((struct eata_cfg *)0L)->ec_cfglen)) +
198 1.4.2.2 bouyer sizeof(ec.ec_cfglen);
199 1.4.2.2 bouyer i >>= 1;
200 1.4.2.2 bouyer
201 1.4.2.2 bouyer while (i--)
202 1.4.2.2 bouyer *p++ = bus_space_read_2(iot, ioh, HA_DATA);
203 1.4.2.2 bouyer
204 1.4.2.2 bouyer /* Flush until we have read 512 bytes. */
205 1.4.2.2 bouyer i = (512 - j + 1) >> 1;
206 1.4.2.2 bouyer while (i--)
207 1.4.2.2 bouyer bus_space_read_2(iot, ioh, HA_DATA);
208 1.4.2.2 bouyer
209 1.4.2.2 bouyer /* Puke if we don't like the returned configuration data. */
210 1.4.2.2 bouyer if ((bus_space_read_1(iot, ioh, HA_STATUS) & HA_ST_ERROR) != 0 ||
211 1.4.2.2 bouyer memcmp(ec.ec_eatasig, "EATA", 4) != 0 ||
212 1.4.2.2 bouyer (ec.ec_feat0 & (EC_F0_HBA_VALID | EC_F0_DMA_SUPPORTED)) !=
213 1.4.2.2 bouyer (EC_F0_HBA_VALID | EC_F0_DMA_SUPPORTED)) {
214 1.4.2.2 bouyer bus_space_unmap(iot, ioh, DPT_ISA_IOSIZE);
215 1.4.2.2 bouyer return (0);
216 1.4.2.2 bouyer }
217 1.4.2.2 bouyer
218 1.4.2.2 bouyer /*
219 1.4.2.2 bouyer * Which DMA channel to use: if it was hardwired in the kernel
220 1.4.2.2 bouyer * configuration, use that value. If the HBA told us, use that
221 1.4.2.2 bouyer * value. Otherwise, puke.
222 1.4.2.2 bouyer */
223 1.4.2.2 bouyer if (ia->ia_drq == -1) {
224 1.4.2.2 bouyer int dmanum = ((ec.ec_feat1 & EC_F1_DMA_NUM_MASK) >>
225 1.4.2.2 bouyer EC_F1_DMA_NUM_SHIFT);
226 1.4.2.2 bouyer
227 1.4.2.2 bouyer if ((ec.ec_feat0 & EC_F0_DMA_NUM_VALID) == 0 || dmanum > 3) {
228 1.4.2.2 bouyer bus_space_unmap(iot, ioh, DPT_ISA_IOSIZE);
229 1.4.2.2 bouyer return (0);
230 1.4.2.2 bouyer }
231 1.4.2.2 bouyer
232 1.4.2.2 bouyer ia->ia_drq = "\0\7\6\5"[dmanum];
233 1.4.2.2 bouyer }
234 1.4.2.2 bouyer
235 1.4.2.2 bouyer /*
236 1.4.2.2 bouyer * Which IRQ to use: if it was hardwired in the kernel configuration,
237 1.4.2.2 bouyer * use that value. Otherwise, use what the HBA told us.
238 1.4.2.2 bouyer */
239 1.4.2.2 bouyer if (ia->ia_irq == -1)
240 1.4.2.2 bouyer ia->ia_irq = ((ec.ec_feat1 & EC_F1_IRQ_NUM_MASK) >>
241 1.4.2.2 bouyer EC_F1_IRQ_NUM_SHIFT);
242 1.4.2.2 bouyer
243 1.4.2.2 bouyer ia->ia_msize = 0;
244 1.4.2.2 bouyer ia->ia_iosize = DPT_ISA_IOSIZE;
245 1.4.2.2 bouyer bus_space_unmap(iot, ioh, DPT_ISA_IOSIZE);
246 1.4.2.2 bouyer return (1);
247 1.4.2.2 bouyer }
248 1.4.2.2 bouyer
249 1.4.2.2 bouyer /*
250 1.4.2.2 bouyer * Attach a matched board.
251 1.4.2.2 bouyer */
252 1.4.2.2 bouyer static void
253 1.4.2.2 bouyer dpt_isa_attach(parent, self, aux)
254 1.4.2.2 bouyer struct device *parent, *self;
255 1.4.2.2 bouyer void *aux;
256 1.4.2.2 bouyer {
257 1.4.2.2 bouyer struct isa_attach_args *ia;
258 1.4.2.2 bouyer isa_chipset_tag_t ic;
259 1.4.2.2 bouyer bus_space_handle_t ioh;
260 1.4.2.2 bouyer bus_space_tag_t iot;
261 1.4.2.2 bouyer struct dpt_softc *sc;
262 1.4.2.2 bouyer struct eata_cfg *ec;
263 1.4.2.2 bouyer int error;
264 1.4.2.2 bouyer
265 1.4.2.2 bouyer ia = aux;
266 1.4.2.2 bouyer sc = (struct dpt_softc *)self;
267 1.4.2.2 bouyer iot = ia->ia_iot;
268 1.4.2.2 bouyer ic = ia->ia_ic;
269 1.4.2.2 bouyer
270 1.4.2.2 bouyer printf(": ");
271 1.4.2.2 bouyer
272 1.4.2.2 bouyer if ((error = bus_space_map(iot, ia->ia_iobase, DPT_ISA_IOSIZE, 0,
273 1.4.2.2 bouyer &ioh)) != 0) {
274 1.4.2.2 bouyer printf("can't map i/o space, error = %d\n", error);
275 1.4.2.2 bouyer return;
276 1.4.2.2 bouyer }
277 1.4.2.2 bouyer
278 1.4.2.2 bouyer sc->sc_iot = iot;
279 1.4.2.2 bouyer sc->sc_ioh = ioh;
280 1.4.2.2 bouyer sc->sc_dmat = ia->ia_dmat;
281 1.4.2.2 bouyer
282 1.4.2.2 bouyer if ((error = isa_dmacascade(ic, ia->ia_drq)) != 0) {
283 1.4.2.2 bouyer printf("unable to cascade DRQ, error = %d\n", error);
284 1.4.2.2 bouyer return;
285 1.4.2.2 bouyer }
286 1.4.2.2 bouyer
287 1.4.2.2 bouyer /* Establish the interrupt. */
288 1.4.2.2 bouyer if ((sc->sc_ih = isa_intr_establish(ic, ia->ia_irq, IST_EDGE, IPL_BIO,
289 1.4.2.2 bouyer dpt_intr, sc)) == NULL) {
290 1.4.2.2 bouyer printf("can't establish interrupt\n");
291 1.4.2.2 bouyer return;
292 1.4.2.2 bouyer }
293 1.4.2.2 bouyer
294 1.4.2.2 bouyer if (dpt_readcfg(sc)) {
295 1.4.2.2 bouyer printf("readcfg failed - see dpt(4)\n");
296 1.4.2.2 bouyer return;
297 1.4.2.2 bouyer }
298 1.4.2.2 bouyer
299 1.4.2.2 bouyer /*
300 1.4.2.2 bouyer * Now attach to the bus-independent code. XXX We need to force
301 1.4.2.2 bouyer * parameters that aren't filled in by some ISA boards. In
302 1.4.2.2 bouyer * particular, due to the limited amount of memory we have to play
303 1.4.2.2 bouyer * with for DMA, clamp the number of CCBs to 16. I don't know if
304 1.4.2.2 bouyer * making the DMA map non-contigiuous would allow us to play with
305 1.4.2.2 bouyer * more CCBs, but in any case that *could* cause a performance hit,
306 1.4.2.2 bouyer * at least for ISA HBAs.
307 1.4.2.2 bouyer */
308 1.4.2.2 bouyer ec = &sc->sc_ec;
309 1.4.2.2 bouyer
310 1.4.2.2 bouyer if (be16toh(*(int16_t *)ec->ec_queuedepth) > DPT_ISA_MAXCCBS)
311 1.4.2.2 bouyer *(int16_t *)ec->ec_queuedepth = htobe16(DPT_ISA_MAXCCBS);
312 1.4.2.2 bouyer if (ec->ec_maxlun == 0)
313 1.4.2.2 bouyer ec->ec_maxlun = 7;
314 1.4.2.2 bouyer if ((ec->ec_feat3 & EC_F3_MAX_TARGET_MASK) >> EC_F3_MAX_TARGET_SHIFT
315 1.4.2.2 bouyer == 0)
316 1.4.2.2 bouyer ec->ec_feat3 = (ec->ec_feat3 & ~EC_F3_MAX_TARGET_MASK) |
317 1.4.2.2 bouyer (7 << EC_F3_MAX_TARGET_SHIFT);
318 1.4.2.2 bouyer
319 1.4.2.2 bouyer /* Now attach to the bus-independent code */
320 1.4.2.2 bouyer dpt_init(sc, NULL);
321 1.4.2.2 bouyer }
322