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