1 /*	$OpenBSD: bha_isa.c,v 1.2 2002/03/14 01:26:56 millert Exp $	*/
2 /*	$NetBSD: bha_isa.c,v 1.14 1998/08/15 10:10:51 mycroft Exp $	*/
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Charles M. Hannum.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 
45 #include <machine/bus.h>
46 #include <machine/intr.h>
47 
48 #include <scsi/scsi_all.h>
49 #include <scsi/scsiconf.h>
50 
51 #include <dev/isa/isavar.h>
52 #include <dev/isa/isadmavar.h>
53 
54 #include <dev/ic/bhareg.h>
55 #include <dev/ic/bhavar.h>
56 
57 #include "aha.h"
58 #if NAHA > 0
59 int btports[8];	/* cannot be more */
60 int nbtports;
61 #endif
62 
63 #define	BHA_ISA_IOSIZE	4
64 
65 int	bha_isa_probe(struct device *, void *, void *);
66 void	bha_isa_attach(struct device *, struct device *, void *);
67 
68 struct cfattach bha_isa_ca = {
69 	sizeof(struct bha_softc), bha_isa_probe, bha_isa_attach
70 };
71 
72 /*
73  * Check the slots looking for a board we recognise
74  * If we find one, note it's address (slot) and call
75  * the actual probe routine to check it out.
76  */
77 int
bha_isa_probe(parent,match,aux)78 bha_isa_probe(parent, match, aux)
79 	struct device *parent;
80 	void *aux, *match;
81 {
82 	struct isa_attach_args *ia = aux;
83 	bus_space_tag_t iot = ia->ia_iot;
84 	bus_space_handle_t ioh;
85 	struct bha_probe_data bpd;
86 #if NAHA > 0
87 	struct bha_digit digit;
88 #endif
89 	int rv;
90 
91 	/* Disallow wildcarded i/o address. */
92 	if (ia->ia_iobase == IOBASEUNK)
93 		return (0);
94 
95 	if (bus_space_map(iot, ia->ia_iobase, BHA_ISA_IOSIZE, 0, &ioh))
96 		return (0);
97 
98 	rv = bha_find(iot, ioh, &bpd);
99 
100 #if NAHA > 0
101 	if (rv) {
102 		/* Adaptec 1542 cards do not support this */
103 		digit.reply.digit = '@';
104 		digit.cmd.opcode = BHA_INQUIRE_REVISION_3;
105 		bha_cmd(iot, ioh, NULL, sizeof(digit.cmd), (u_char *)&digit.cmd,
106 		    sizeof(digit.reply), (u_char *)&digit.reply);
107 		if (digit.reply.digit == '@')
108 			return 1;
109 	}
110 #endif
111 
112 	bus_space_unmap(iot, ioh, BHA_ISA_IOSIZE);
113 
114 	if (rv) {
115 		if (ia->ia_irq != -1 && ia->ia_irq != bpd.sc_irq)
116 			return (0);
117 		if (ia->ia_drq != -1 && ia->ia_drq != bpd.sc_drq)
118 			return (0);
119 		ia->ia_irq = bpd.sc_irq;
120 		ia->ia_drq = bpd.sc_drq;
121 		ia->ia_msize = 0;
122 		ia->ia_iosize = BHA_ISA_IOSIZE;
123 	}
124 	return (rv);
125 }
126 
127 /*
128  * Attach all the sub-devices we can find
129  */
130 void
bha_isa_attach(parent,self,aux)131 bha_isa_attach(parent, self, aux)
132 	struct device *parent, *self;
133 	void *aux;
134 {
135 	struct isa_attach_args *ia = aux;
136 	struct bha_softc *sc = (void *)self;
137 	bus_space_tag_t iot = ia->ia_iot;
138 	bus_space_handle_t ioh;
139 	struct bha_probe_data bpd;
140 	isa_chipset_tag_t ic = ia->ia_ic;
141 #ifndef __OpenBSD__
142 	int error;
143 #endif
144 
145 	printf("\n");
146 
147 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
148 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
149 		return;
150 	}
151 
152 	sc->sc_iot = iot;
153 	sc->sc_ioh = ioh;
154 	sc->sc_dmat = ia->ia_dmat;
155 	if (!bha_find(iot, ioh, &bpd)) {
156 		printf("%s: bha_find failed\n", sc->sc_dev.dv_xname);
157 		return;
158 	}
159 
160 	sc->sc_dmaflags = 0;
161 	if (bpd.sc_drq != DRQUNK) {
162 #ifdef __OpenBSD__
163 		isa_dmacascade(ic, bpd.sc_drq);
164 #else
165 		if ((error = isa_dmacascade(ic, bpd.sc_drq)) != 0) {
166 			printf("%s: unable to cascade DRQ, error = %d\n",
167 			    sc->sc_dev.dv_xname, error);
168 			return;
169 		}
170 #endif
171 	} else {
172 		/*
173 		 * We have a VLB controller.  If we're at least both
174 		 * hardware revision E and firmware revision 3.37,
175 		 * we can do 32-bit DMA (earlier revisions are buggy
176 		 * in this regard).
177 		 */
178 		bha_inquire_setup_information(sc);
179 		if (strcmp(sc->sc_firmware, "3.37") < 0)
180 		    printf("%s: buggy VLB controller, disabling 32-bit DMA\n",
181 		        sc->sc_dev.dv_xname);
182 		else
183 			sc->sc_dmaflags = ISABUS_DMA_32BIT;
184 	}
185 
186 	sc->sc_ih = isa_intr_establish(ic, bpd.sc_irq, IST_EDGE, IPL_BIO,
187 	    bha_intr, sc, sc->sc_dev.dv_xname);
188 	if (sc->sc_ih == NULL) {
189 		printf("%s: couldn't establish interrupt\n",
190 		    sc->sc_dev.dv_xname);
191 		return;
192 	}
193 
194 	bha_attach(sc, &bpd);
195 
196 #if NAHA > 0
197 	/* XXXX To avoid conflicting with the aha1542 probe */
198 	btports[nbtports++] = ia->ia_iobase;
199 #endif
200 }
201