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