xref: /netbsd-src/sys/dev/isa/nca_isa.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: nca_isa.c,v 1.19 2007/10/19 12:00:21 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by John M. Ruschmeyer.
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  * FreeBSD generic NCR-5380/NCR-53C400 SCSI driver
41  *
42  * Copyright (C) 1994 Serge Vakulenko (vak@cronyx.ru)
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPERS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  */
65 
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: nca_isa.c,v 1.19 2007/10/19 12:00:21 ad Exp $");
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/device.h>
72 #include <sys/buf.h>
73 
74 #include <sys/bus.h>
75 #include <sys/intr.h>
76 
77 #include <dev/scsipi/scsi_all.h>
78 #include <dev/scsipi/scsipi_all.h>
79 #include <dev/scsipi/scsiconf.h>
80 
81 #include <dev/isa/isavar.h>
82 #include <dev/isa/isadmavar.h>
83 
84 #include <dev/ic/ncr5380reg.h>
85 #include <dev/ic/ncr5380var.h>
86 #include <dev/ic/ncr53c400reg.h>
87 
88 struct nca_isa_softc {
89 	struct ncr5380_softc	sc_ncr5380;	/* glue to MI code */
90 
91         void *sc_ih;
92         int sc_irq;
93 	int sc_options;
94 };
95 
96 struct nca_isa_probe_data {
97 	struct device sc_dev;
98 	int sc_reg_offset;
99 	int sc_host_type;
100 };
101 
102 int	nca_isa_find(bus_space_tag_t, bus_space_handle_t, bus_size_t,
103 	    struct nca_isa_probe_data *);
104 int	nca_isa_match(struct device *, struct cfdata *, void *);
105 void	nca_isa_attach(struct device *, struct device *, void *);
106 int	nca_isa_test(bus_space_tag_t, bus_space_handle_t, bus_size_t);
107 
108 CFATTACH_DECL(nca_isa, sizeof(struct nca_isa_softc),
109     nca_isa_match, nca_isa_attach, NULL, NULL);
110 
111 
112 /* Supported controller types */
113 #define MAX_NCA_CONTROLLER	3
114 #define CTLR_NCR_5380	1
115 #define	CTLR_NCR_53C400	2
116 #define CTLR_PAS16	3
117 
118 #define NCA_ISA_IOSIZE 16
119 #define MIN_DMA_LEN 128
120 
121 /* Options for disconnect/reselect, DMA, and interrupts. */
122 #define NCA_NO_DISCONNECT    0xff
123 #define NCA_NO_PARITY_CHK  0xff00
124 #define NCA_FORCE_POLLING 0x10000
125 
126 
127 /*
128  * Initialization and test function used by nca_isa_find()
129  */
130 int
131 nca_isa_test(iot, ioh, reg_offset)
132 	bus_space_tag_t	iot;
133 	bus_space_handle_t ioh;
134 	bus_size_t reg_offset;
135 {
136 	/* Reset the SCSI bus. */
137 	bus_space_write_1(iot, ioh, reg_offset + C80_ICR, SCI_ICMD_RST);
138 	bus_space_write_1(iot, ioh, reg_offset + C80_ODR, 0);
139 	/* Hold reset for at least 25 microseconds. */
140 	delay(500);
141 	/* Check that status cleared. */
142 	if (bus_space_read_1(iot, ioh, reg_offset + C80_CSBR) != SCI_BUS_RST) {
143 #ifdef DEBUG
144 		printf("nca_isa_find: reset status not cleared [0x%x]\n",
145 		    bus_space_read_1(iot, ioh, reg_offset+C80_CSBR));
146 #endif
147 		bus_space_write_1(iot, ioh, reg_offset+C80_ICR, 0);
148 		return 0;
149 	}
150 	/* Clear reset. */
151 	bus_space_write_1(iot, ioh, reg_offset + C80_ICR, 0);
152 	/* Wait a Bus Clear Delay (800 ns + bus free delay 800 ns). */
153 	delay(16000);
154 
155 	/* Read RPI port, resetting parity/interrupt state. */
156 	bus_space_read_1(iot, ioh, reg_offset + C80_RPIR);
157 
158 	/* Test BSR: parity error, interrupt request and busy loss state
159 	 * should be cleared. */
160 	if (bus_space_read_1(iot, ioh, reg_offset + C80_BSR) & (SCI_CSR_PERR |
161 	    SCI_CSR_INT | SCI_CSR_DISC)) {
162 #ifdef DEBUG
163 		printf("nca_isa_find: Parity/Interrupt/Busy not cleared [0x%x]\n",
164 		    bus_space_read_1(iot, ioh, reg_offset+C80_BSR));
165 #endif
166 		return 0;
167 	}
168 
169 	/* We must have found one */
170 	return 1;
171 }
172 
173 
174 /*
175  * Look for the board
176  */
177 int
178 nca_isa_find(iot, ioh, max_offset, epd)
179 	bus_space_tag_t iot;
180 	bus_space_handle_t ioh;
181 	bus_size_t max_offset;
182 	struct nca_isa_probe_data *epd;
183 {
184 	/*
185 	 * We check for the existence of a board by trying to initialize it,
186 	 * Then sending the commands to reset the SCSI bus.
187 	 * (Unfortunately, this duplicates code which is already in the MI
188 	 * driver. Unavoidable as that code is not suited to this task.)
189 	 * This is largely stolen from FreeBSD.
190 	 */
191 
192 	int 		cont_type;
193 	bus_size_t	base_offset, reg_offset = 0;
194 
195 	/*
196 	 * Some notes:
197 	 * In the case of a port-mapped board, we should be pointing
198 	 * right at the chip registers (if they are there at all).
199 	 * For a memory-mapped card, we loop through the 16K paragraph,
200 	 * 8 bytes at a time, until we either find it or run out
201 	 * of region. This means we will probably be doing things like
202 	 * trying to write to ROMS, etc. Hopefully, this is not a problem.
203 	 */
204 
205 	for (base_offset = 0; base_offset < max_offset; base_offset += 0x08) {
206 #ifdef DEBUG
207 		printf("nca_isa_find: testing offset 0x%x\n", (int)base_offset);
208 #endif
209 
210 		/* See if anything is there */
211 		if (bus_space_read_1(iot, ioh, base_offset) == 0xff)
212 			continue;
213 
214 		/* Loop around for each board type */
215 		for (cont_type = 1; cont_type <= MAX_NCA_CONTROLLER; cont_type++) {
216 			/* Per-controller initialization */
217 			switch (cont_type) {
218 			case CTLR_NCR_5380:
219 				/* No special inits */
220 				reg_offset = 0;
221 				break;
222 			case CTLR_NCR_53C400:
223 				/* Reset into 5380-compat. mode */
224 				bus_space_write_1(iot, ioh,
225 				    base_offset + C400_CSR,
226 				    C400_CSR_5380_ENABLE);
227 				reg_offset = C400_5380_REG_OFFSET;
228 				break;
229 			case CTLR_PAS16:
230 				/* Not currently supported */
231 				reg_offset = 0;
232 				cont_type = 0;
233 				continue;
234 			}
235 
236 			/* Initialize controller and bus */
237 			if (nca_isa_test(iot, ioh, base_offset+reg_offset)) {
238 				epd->sc_reg_offset = base_offset;
239 				epd->sc_host_type = cont_type;
240 				return cont_type;	/* This must be it */
241 			}
242 		}
243 	}
244 
245 	/* If we got here, we didn't find one */
246 	return 0;
247 }
248 
249 
250 /*
251  * See if there is anything at the config'd address.
252  * If so, call the real probe to see what it is.
253  */
254 int
255 nca_isa_match(struct device *parent, struct cfdata *match,
256     void *aux)
257 {
258 	struct isa_attach_args *ia = aux;
259 	bus_space_tag_t iot = ia->ia_iot;
260 	bus_space_tag_t memt = ia->ia_memt;
261 	bus_space_handle_t ioh;
262 	struct nca_isa_probe_data epd;
263 	int rv = 0;
264 
265 	if (ISA_DIRECT_CONFIG(ia))
266 		return (0);
267 
268 	/* See if we are looking for a port- or memory-mapped adapter */
269 	if (ia->ia_nio > 0 || ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT) {
270 		/* Port-mapped card */
271 		if (bus_space_map(iot, ia->ia_io[0].ir_addr, NCA_ISA_IOSIZE,
272 		    0, &ioh))
273 			return 0;
274 
275 		/* See if a 53C80/53C400 is there */
276 		rv = nca_isa_find(iot, ioh, 0x07, &epd);
277 
278 		bus_space_unmap(iot, ioh, NCA_ISA_IOSIZE);
279 
280 		if (rv) {
281 			ia->ia_nio = 1;
282 			ia->ia_io[0].ir_size = NCA_ISA_IOSIZE;
283 
284 			ia->ia_niomem = 0;
285 			ia->ia_ndrq = 0;
286 		}
287 	} else if (ia->ia_niomem > 0) {
288 		/* Memory-mapped card */
289 		if (bus_space_map(memt, ia->ia_iomem[0].ir_addr, 0x4000,
290 		    0, &ioh))
291 			return 0;
292 
293 		/* See if a 53C80/53C400 is somewhere in this para. */
294 		rv = nca_isa_find(memt, ioh, 0x03ff0, &epd);
295 
296 		bus_space_unmap(memt, ioh, 0x04000);
297 
298 		if (rv) {
299 			ia->ia_niomem = 1;
300 			ia->ia_iomem[0].ir_addr += epd.sc_reg_offset;
301 			ia->ia_iomem[0].ir_size = NCA_ISA_IOSIZE;
302 
303 			ia->ia_nio = 0;
304 			ia->ia_ndrq = 0;
305 		}
306 	}
307 
308 	return rv;
309 }
310 
311 /*
312  * Attach this instance, and then all the sub-devices
313  */
314 void
315 nca_isa_attach(struct device *parent, struct device *self, void *aux)
316 {
317 	struct isa_attach_args *ia = aux;
318 	struct nca_isa_softc *esc = (void *)self;
319 	struct ncr5380_softc *sc = &esc->sc_ncr5380;
320 	bus_space_tag_t iot = ia->ia_iot;
321 	bus_space_handle_t ioh;
322 	struct nca_isa_probe_data epd;
323 	isa_chipset_tag_t ic = ia->ia_ic;
324 
325 	printf("\n");
326 
327 	if (ia->ia_nio > 0) {
328 		iot = ia->ia_iot;
329 		if (bus_space_map(iot, ia->ia_io[0].ir_addr, NCA_ISA_IOSIZE,
330 		    0, &ioh)) {
331 			printf("%s: can't map i/o space\n",
332 			    sc->sc_dev.dv_xname);
333 			return;
334 		}
335 	} else {
336 		KASSERT(ia->ia_niomem > 0);
337 		iot = ia->ia_memt;
338 		if (bus_space_map(iot, ia->ia_iomem[0].ir_addr, NCA_ISA_IOSIZE,
339 		    0, &ioh)) {
340 			printf("%s: can't map mem space\n",
341 			    sc->sc_dev.dv_xname);
342 			return;
343 		}
344 	}
345 
346 	switch (nca_isa_find(iot, ioh, NCA_ISA_IOSIZE, &epd)) {
347 	case 0:
348 		/* Not found- must have gone away */
349 		printf("%s: nca_isa_find failed\n", sc->sc_dev.dv_xname);
350 		return;
351 	case CTLR_NCR_5380:
352 		printf("%s: NCR 53C80 detected\n", sc->sc_dev.dv_xname);
353 		sc->sci_r0 = 0;
354 		sc->sci_r1 = 1;
355 		sc->sci_r2 = 2;
356 		sc->sci_r3 = 3;
357 		sc->sci_r4 = 4;
358 		sc->sci_r5 = 5;
359 		sc->sci_r6 = 6;
360 		sc->sci_r7 = 7;
361 		sc->sc_rev = NCR_VARIANT_NCR5380;
362 		break;
363 	case CTLR_NCR_53C400:
364 		printf("%s: NCR 53C400 detected\n", sc->sc_dev.dv_xname);
365 		sc->sci_r0 = C400_5380_REG_OFFSET + 0;
366 		sc->sci_r1 = C400_5380_REG_OFFSET + 1;
367 		sc->sci_r2 = C400_5380_REG_OFFSET + 2;
368 		sc->sci_r3 = C400_5380_REG_OFFSET + 3;
369 		sc->sci_r4 = C400_5380_REG_OFFSET + 4;
370 		sc->sci_r5 = C400_5380_REG_OFFSET + 5;
371 		sc->sci_r6 = C400_5380_REG_OFFSET + 6;
372 		sc->sci_r7 = C400_5380_REG_OFFSET + 7;
373 		sc->sc_rev = NCR_VARIANT_NCR53C400;
374 		break;
375 	case CTLR_PAS16:
376 		printf("%s: ProAudio Spectrum 16 detected\n",
377 		    sc->sc_dev.dv_xname);
378 		sc->sc_rev = NCR_VARIANT_PAS16;
379 		break;
380 	}
381 
382 	/*
383 	 * MD function pointers used by the MI code.
384 	 */
385 	sc->sc_pio_out = ncr5380_pio_out;
386 	sc->sc_pio_in =  ncr5380_pio_in;
387 	sc->sc_dma_alloc = NULL;
388 	sc->sc_dma_free  = NULL;
389 	sc->sc_dma_setup = NULL;
390 	sc->sc_dma_start = NULL;
391 	sc->sc_dma_poll  = NULL;
392 	sc->sc_dma_eop   = NULL;
393 	sc->sc_dma_stop  = NULL;
394 	sc->sc_intr_on   = NULL;
395 	sc->sc_intr_off  = NULL;
396 
397 	if (ia->ia_nirq > 0 && ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ) {
398 		esc->sc_ih = isa_intr_establish(ic, ia->ia_irq[0].ir_irq,
399 		    IST_EDGE, IPL_BIO, ncr5380_intr, esc);
400 		if (esc->sc_ih == NULL) {
401 			printf("%s: couldn't establish interrupt\n",
402 			    sc->sc_dev.dv_xname);
403 			return;
404 		}
405 	} else
406 		sc->sc_flags |= NCR5380_FORCE_POLLING;
407 
408 
409 	/*
410 	 * Support the "options" (config file flags).
411 	 * Disconnect/reselect is a per-target mask.
412 	 * Interrupts and DMA are per-controller.
413 	 */
414 #if 0
415 	esc->sc_options = 0x00000;	/* no options */
416 #else
417 	esc->sc_options = 0x0ffff;	/* all options except force poll */
418 #endif
419 
420 	sc->sc_no_disconnect =
421 		(esc->sc_options & NCA_NO_DISCONNECT);
422 	sc->sc_parity_disable =
423 		(esc->sc_options & NCA_NO_PARITY_CHK) >> 8;
424 	if (esc->sc_options & NCA_FORCE_POLLING)
425 		sc->sc_flags |= NCR5380_FORCE_POLLING;
426 	sc->sc_min_dma_len = MIN_DMA_LEN;
427 
428 
429 	/*
430 	 * Initialize fields used by the MI code
431 	 */
432 	sc->sc_regt = iot;
433 	sc->sc_regh = ioh;
434 
435 	/*
436 	 * Fill in our portion of the scsipi_adapter.
437 	 */
438 	sc->sc_adapter.adapt_request = ncr5380_scsipi_request;
439 	sc->sc_adapter.adapt_minphys = minphys;
440 
441 	/*
442 	 * Fill in our portion of the scsipi_channel.
443 	 */
444 
445 	sc->sc_channel.chan_id = 7;
446 
447 	/*
448 	 *  Initialize nca board itself.
449 	 */
450 	ncr5380_attach(sc);
451 }
452