xref: /freebsd/sys/dev/aic7xxx/aic7770.c (revision a3e8fd0b7f663db7eafff527d5c3ca3bcfa8a537)
1 /*
2  * Product specific probe and attach routines for:
3  * 	27/284X and aic7770 motherboard SCSI controllers
4  *
5  * Copyright (c) 1994-1998, 2000, 2001 Justin T. Gibbs.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification.
14  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15  *    substantially similar to the "NO WARRANTY" disclaimer below
16  *    ("Disclaimer") and any redistribution must be conditioned upon
17  *    including a substantially similar Disclaimer requirement for further
18  *    binary redistribution.
19  * 3. Neither the names of the above-listed copyright holders nor the names
20  *    of any contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * Alternatively, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") version 2 as published by the Free
25  * Software Foundation.
26  *
27  * NO WARRANTY
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGES.
39  *
40  * $Id: //depot/aic7xxx/aic7xxx/aic7770.c#26 $
41  *
42  * $FreeBSD$
43  */
44 
45 #ifdef __linux__
46 #include "aic7xxx_osm.h"
47 #include "aic7xxx_inline.h"
48 #include "aic7xxx_93cx6.h"
49 #else
50 #include <dev/aic7xxx/aic7xxx_osm.h>
51 #include <dev/aic7xxx/aic7xxx_inline.h>
52 #include <dev/aic7xxx/aic7xxx_93cx6.h>
53 #endif
54 
55 #define ID_AIC7770	0x04907770
56 #define ID_AHA_274x	0x04907771
57 #define ID_AHA_284xB	0x04907756 /* BIOS enabled */
58 #define ID_AHA_284x	0x04907757 /* BIOS disabled*/
59 
60 static int aha2840_load_seeprom(struct ahc_softc *ahc);
61 static ahc_device_setup_t ahc_aic7770_VL_setup;
62 static ahc_device_setup_t ahc_aic7770_EISA_setup;;
63 static ahc_device_setup_t ahc_aic7770_setup;
64 
65 
66 struct aic7770_identity aic7770_ident_table [] =
67 {
68 	{
69 		ID_AHA_274x,
70 		0xFFFFFFFF,
71 		"Adaptec 274X SCSI adapter",
72 		ahc_aic7770_EISA_setup
73 	},
74 	{
75 		ID_AHA_284xB,
76 		0xFFFFFFFE,
77 		"Adaptec 284X SCSI adapter",
78 		ahc_aic7770_VL_setup
79 	},
80 	/* Generic chip probes for devices we don't know 'exactly' */
81 	{
82 		ID_AIC7770,
83 		0xFFFFFFFF,
84 		"Adaptec aic7770 SCSI adapter",
85 		ahc_aic7770_EISA_setup
86 	}
87 };
88 const int ahc_num_aic7770_devs = NUM_ELEMENTS(aic7770_ident_table);
89 
90 struct aic7770_identity *
91 aic7770_find_device(uint32_t id)
92 {
93 	struct	aic7770_identity *entry;
94 	int	i;
95 
96 	for (i = 0; i < ahc_num_aic7770_devs; i++) {
97 		entry = &aic7770_ident_table[i];
98 		if (entry->full_id == (id & entry->id_mask))
99 			return (entry);
100 	}
101 	return (NULL);
102 }
103 
104 int
105 aic7770_config(struct ahc_softc *ahc, struct aic7770_identity *entry, u_int io)
106 {
107 	u_long	l;
108 	int	error;
109 	int	have_seeprom;
110 	u_int	hostconf;
111 	u_int   irq;
112 	u_int	intdef;
113 
114 	error = entry->setup(ahc);
115 	have_seeprom = 0;
116 	if (error != 0)
117 		return (error);
118 
119 	error = aic7770_map_registers(ahc, io);
120 	if (error != 0)
121 		return (error);
122 
123 	/*
124 	 * Before we continue probing the card, ensure that
125 	 * its interrupts are *disabled*.  We don't want
126 	 * a misstep to hang the machine in an interrupt
127 	 * storm.
128 	 */
129 	ahc_intr_enable(ahc, FALSE);
130 
131 	ahc->description = entry->name;
132 	error = ahc_softc_init(ahc);
133 
134 	error = ahc_reset(ahc);
135 	if (error != 0)
136 		return (error);
137 
138 	/* Make sure we have a valid interrupt vector */
139 	intdef = ahc_inb(ahc, INTDEF);
140 	irq = intdef & VECTOR;
141 	switch (irq) {
142 	case 9:
143 	case 10:
144 	case 11:
145 	case 12:
146 	case 14:
147 	case 15:
148 		break;
149 	default:
150 		printf("aic7770_config: illegal irq setting %d\n", intdef);
151 		return (ENXIO);
152 	}
153 
154 	if ((intdef & EDGE_TRIG) != 0)
155 		ahc->flags |= AHC_EDGE_INTERRUPT;
156 
157 	switch (ahc->chip & (AHC_EISA|AHC_VL)) {
158 	case AHC_EISA:
159 	{
160 		u_int biosctrl;
161 		u_int scsiconf;
162 		u_int scsiconf1;
163 
164 		biosctrl = ahc_inb(ahc, HA_274_BIOSCTRL);
165 		scsiconf = ahc_inb(ahc, SCSICONF);
166 		scsiconf1 = ahc_inb(ahc, SCSICONF + 1);
167 
168 		/* Get the primary channel information */
169 		if ((biosctrl & CHANNEL_B_PRIMARY) != 0)
170 			ahc->flags |= 1;
171 
172 		if ((biosctrl & BIOSMODE) == BIOSDISABLED) {
173 			ahc->flags |= AHC_USEDEFAULTS;
174 		} else {
175 			if ((ahc->features & AHC_WIDE) != 0) {
176 				ahc->our_id = scsiconf1 & HWSCSIID;
177 				if (scsiconf & TERM_ENB)
178 					ahc->flags |= AHC_TERM_ENB_A;
179 			} else {
180 				ahc->our_id = scsiconf & HSCSIID;
181 				ahc->our_id_b = scsiconf1 & HSCSIID;
182 				if (scsiconf & TERM_ENB)
183 					ahc->flags |= AHC_TERM_ENB_A;
184 				if (scsiconf1 & TERM_ENB)
185 					ahc->flags |= AHC_TERM_ENB_B;
186 			}
187 		}
188 		if ((ahc_inb(ahc, HA_274_BIOSGLOBAL) & HA_274_EXTENDED_TRANS))
189 			ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B;
190 		break;
191 	}
192 	case AHC_VL:
193 	{
194 		have_seeprom = aha2840_load_seeprom(ahc);
195 		break;
196 	}
197 	default:
198 		break;
199 	}
200 	if (have_seeprom == 0) {
201 		free(ahc->seep_config, M_DEVBUF);
202 		ahc->seep_config = NULL;
203 	}
204 
205 	/*
206 	 * Ensure autoflush is enabled
207 	 */
208 	ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~AUTOFLUSHDIS);
209 
210 	/* Setup the FIFO threshold and the bus off time */
211 	hostconf = ahc_inb(ahc, HOSTCONF);
212 	ahc_outb(ahc, BUSSPD, hostconf & DFTHRSH);
213 	ahc_outb(ahc, BUSTIME, (hostconf << 2) & BOFF);
214 
215 	/*
216 	 * Generic aic7xxx initialization.
217 	 */
218 	error = ahc_init(ahc);
219 	if (error != 0)
220 		return (error);
221 
222 	error = aic7770_map_int(ahc, irq);
223 	if (error != 0)
224 		return (error);
225 
226 	ahc_list_lock(&l);
227 	/*
228 	 * Link this softc in with all other ahc instances.
229 	 */
230 	ahc_softc_insert(ahc);
231 
232 	/*
233 	 * Enable the board's BUS drivers
234 	 */
235 	ahc_outb(ahc, BCTL, ENABLE);
236 
237 	ahc_list_unlock(&l);
238 
239 	return (0);
240 }
241 
242 /*
243  * Read the 284x SEEPROM.
244  */
245 static int
246 aha2840_load_seeprom(struct ahc_softc *ahc)
247 {
248 	struct	seeprom_descriptor sd;
249 	struct	seeprom_config *sc;
250 	int	have_seeprom;
251 	uint8_t scsi_conf;
252 
253 	sd.sd_ahc = ahc;
254 	sd.sd_control_offset = SEECTL_2840;
255 	sd.sd_status_offset = STATUS_2840;
256 	sd.sd_dataout_offset = STATUS_2840;
257 	sd.sd_chip = C46;
258 	sd.sd_MS = 0;
259 	sd.sd_RDY = EEPROM_TF;
260 	sd.sd_CS = CS_2840;
261 	sd.sd_CK = CK_2840;
262 	sd.sd_DO = DO_2840;
263 	sd.sd_DI = DI_2840;
264 	sc = ahc->seep_config;
265 
266 	if (bootverbose)
267 		printf("%s: Reading SEEPROM...", ahc_name(ahc));
268 	have_seeprom = ahc_read_seeprom(&sd, (uint16_t *)sc,
269 					/*start_addr*/0, sizeof(sc)/2);
270 
271 	if (have_seeprom) {
272 
273 		if (ahc_verify_cksum(sc) == 0) {
274 			if(bootverbose)
275 				printf ("checksum error\n");
276 			have_seeprom = 0;
277 		} else if (bootverbose) {
278 			printf("done.\n");
279 		}
280 	}
281 
282 	if (!have_seeprom) {
283 		if (bootverbose)
284 			printf("%s: No SEEPROM available\n", ahc_name(ahc));
285 		ahc->flags |= AHC_USEDEFAULTS;
286 	} else {
287 		/*
288 		 * Put the data we've collected down into SRAM
289 		 * where ahc_init will find it.
290 		 */
291 		int	 i;
292 		int	 max_targ;
293 		uint16_t discenable;
294 
295 		max_targ = (ahc->features & AHC_WIDE) != 0 ? 16 : 8;
296 		discenable = 0;
297 		for (i = 0; i < max_targ; i++){
298 			uint8_t target_settings;
299 
300 			target_settings = (sc->device_flags[i] & CFXFER) << 4;
301 			if (sc->device_flags[i] & CFSYNCH)
302 				target_settings |= SOFS;
303 			if (sc->device_flags[i] & CFWIDEB)
304 				target_settings |= WIDEXFER;
305 			if (sc->device_flags[i] & CFDISC)
306 				discenable |= (0x01 << i);
307 			ahc_outb(ahc, TARG_SCSIRATE + i, target_settings);
308 		}
309 		ahc_outb(ahc, DISC_DSB, ~(discenable & 0xff));
310 		ahc_outb(ahc, DISC_DSB + 1, ~((discenable >> 8) & 0xff));
311 
312 		ahc->our_id = sc->brtime_id & CFSCSIID;
313 
314 		scsi_conf = (ahc->our_id & 0x7);
315 		if (sc->adapter_control & CFSPARITY)
316 			scsi_conf |= ENSPCHK;
317 		if (sc->adapter_control & CFRESETB)
318 			scsi_conf |= RESET_SCSI;
319 
320 		if (sc->bios_control & CF284XEXTEND)
321 			ahc->flags |= AHC_EXTENDED_TRANS_A;
322 		/* Set SCSICONF info */
323 		ahc_outb(ahc, SCSICONF, scsi_conf);
324 
325 		if (sc->adapter_control & CF284XSTERM)
326 			ahc->flags |= AHC_TERM_ENB_A;
327 	}
328 	return (have_seeprom);
329 }
330 
331 static int
332 ahc_aic7770_VL_setup(struct ahc_softc *ahc)
333 {
334 	int error;
335 
336 	error = ahc_aic7770_setup(ahc);
337 	ahc->chip |= AHC_VL;
338 	return (error);
339 }
340 
341 static int
342 ahc_aic7770_EISA_setup(struct ahc_softc *ahc)
343 {
344 	int error;
345 
346 	error = ahc_aic7770_setup(ahc);
347 	ahc->chip |= AHC_EISA;
348 	return (error);
349 }
350 
351 static int
352 ahc_aic7770_setup(struct ahc_softc *ahc)
353 {
354 	ahc->channel = 'A';
355 	ahc->channel_b = 'B';
356 	ahc->chip = AHC_AIC7770;
357 	ahc->features = AHC_AIC7770_FE;
358 	ahc->bugs |= AHC_TMODE_WIDEODD_BUG;
359 	ahc->flags |= AHC_PAGESCBS;
360 	return (0);
361 }
362