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__FBSDID("$FreeBSD$");
52#include <dev/aic7xxx/aic7xxx_osm.h>
53#include <dev/aic7xxx/aic7xxx_inline.h>
54#include <dev/aic7xxx/aic7xxx_93cx6.h>
55#endif
56
57#define ID_AIC7770	0x04907770
58#define ID_AHA_274x	0x04907771
59#define ID_AHA_284xB	0x04907756 /* BIOS enabled */
60#define ID_AHA_284x	0x04907757 /* BIOS disabled*/
61#define	ID_OLV_274x	0x04907782 /* Olivetti OEM */
62#define	ID_OLV_274xD	0x04907783 /* Olivetti OEM (Differential) */
63
64static int aic7770_chip_init(struct ahc_softc *ahc);
65static int aic7770_suspend(struct ahc_softc *ahc);
66static int aic7770_resume(struct ahc_softc *ahc);
67static int aha2840_load_seeprom(struct ahc_softc *ahc);
68static ahc_device_setup_t ahc_aic7770_VL_setup;
69static ahc_device_setup_t ahc_aic7770_EISA_setup;	/* Really just ISA */
70static ahc_device_setup_t ahc_aic7770_setup;
71
72struct aic7770_identity aic7770_ident_table[] =
73{
74	{
75		ID_AHA_274x,
76		0xFFFFFFFF,
77		"Adaptec 274X SCSI adapter",
78		ahc_aic7770_EISA_setup
79	},
80	{
81		ID_AHA_284xB,
82		0xFFFFFFFE,
83		"Adaptec 284X SCSI adapter",
84		ahc_aic7770_VL_setup
85	},
86	{
87		ID_AHA_284x,
88		0xFFFFFFFE,
89		"Adaptec 284X SCSI adapter (BIOS Disabled)",
90		ahc_aic7770_VL_setup
91	},
92	{
93		ID_OLV_274x,
94		0xFFFFFFFF,
95		"Adaptec (Olivetti OEM) 274X SCSI adapter",
96		ahc_aic7770_EISA_setup
97	},
98	{
99		ID_OLV_274xD,
100		0xFFFFFFFF,
101		"Adaptec (Olivetti OEM) 274X Differential SCSI adapter",
102		ahc_aic7770_EISA_setup
103	},
104	/* Generic chip probes for devices we don't know 'exactly' */
105	{
106		ID_AIC7770,
107		0xFFFFFFFF,
108		"Adaptec aic7770 SCSI adapter",
109		ahc_aic7770_EISA_setup
110	}
111};
112const int ahc_num_aic7770_devs = NUM_ELEMENTS(aic7770_ident_table);
113
114struct aic7770_identity *
115aic7770_find_device(uint32_t id)
116{
117	struct	aic7770_identity *entry;
118	int	i;
119
120	for (i = 0; i < ahc_num_aic7770_devs; i++) {
121		entry = &aic7770_ident_table[i];
122		if (entry->full_id == (id & entry->id_mask))
123			return (entry);
124	}
125	return (NULL);
126}
127
128int
129aic7770_config(struct ahc_softc *ahc, struct aic7770_identity *entry, u_int io)
130{
131	int	error;
132	int	have_seeprom;
133	u_int	hostconf;
134	u_int   irq;
135	u_int	intdef;
136
137	error = entry->setup(ahc);
138	have_seeprom = 0;
139	if (error != 0)
140		return (error);
141
142	error = aic7770_map_registers(ahc, io);
143	if (error != 0)
144		return (error);
145
146	/*
147	 * Before we continue probing the card, ensure that
148	 * its interrupts are *disabled*.  We don't want
149	 * a misstep to hang the machine in an interrupt
150	 * storm.
151	 */
152	ahc_intr_enable(ahc, FALSE);
153
154	ahc->description = entry->name;
155	error = ahc_softc_init(ahc);
156	if (error != 0)
157		return (error);
158
159	ahc->bus_chip_init = aic7770_chip_init;
160	ahc->bus_suspend = aic7770_suspend;
161	ahc->bus_resume = aic7770_resume;
162
163	error = ahc_reset(ahc, /*reinit*/FALSE);
164	if (error != 0)
165		return (error);
166
167	/* Make sure we have a valid interrupt vector */
168	intdef = ahc_inb(ahc, INTDEF);
169	irq = intdef & VECTOR;
170	switch (irq) {
171	case 9:
172	case 10:
173	case 11:
174	case 12:
175	case 14:
176	case 15:
177		break;
178	default:
179		printf("aic7770_config: invalid irq setting %d\n", intdef);
180		return (ENXIO);
181	}
182
183	if ((intdef & EDGE_TRIG) != 0)
184		ahc->flags |= AHC_EDGE_INTERRUPT;
185
186	switch (ahc->chip & (AHC_EISA|AHC_VL)) {
187	case AHC_EISA:
188	{
189		u_int biosctrl;
190		u_int scsiconf;
191		u_int scsiconf1;
192
193		biosctrl = ahc_inb(ahc, HA_274_BIOSCTRL);
194		scsiconf = ahc_inb(ahc, SCSICONF);
195		scsiconf1 = ahc_inb(ahc, SCSICONF + 1);
196
197		/* Get the primary channel information */
198		if ((biosctrl & CHANNEL_B_PRIMARY) != 0)
199			ahc->flags |= 1;
200
201		if ((biosctrl & BIOSMODE) == BIOSDISABLED) {
202			ahc->flags |= AHC_USEDEFAULTS;
203		} else {
204			if ((ahc->features & AHC_WIDE) != 0) {
205				ahc->our_id = scsiconf1 & HWSCSIID;
206				if (scsiconf & TERM_ENB)
207					ahc->flags |= AHC_TERM_ENB_A;
208			} else {
209				ahc->our_id = scsiconf & HSCSIID;
210				ahc->our_id_b = scsiconf1 & HSCSIID;
211				if (scsiconf & TERM_ENB)
212					ahc->flags |= AHC_TERM_ENB_A;
213				if (scsiconf1 & TERM_ENB)
214					ahc->flags |= AHC_TERM_ENB_B;
215			}
216		}
217		if ((ahc_inb(ahc, HA_274_BIOSGLOBAL) & HA_274_EXTENDED_TRANS))
218			ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B;
219		break;
220	}
221	case AHC_VL:
222	{
223		have_seeprom = aha2840_load_seeprom(ahc);
224		break;
225	}
226	default:
227		break;
228	}
229	if (have_seeprom == 0) {
230		free(ahc->seep_config, M_DEVBUF);
231		ahc->seep_config = NULL;
232	}
233
234	/*
235	 * Ensure autoflush is enabled
236	 */
237	ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~AUTOFLUSHDIS);
238
239	/* Setup the FIFO threshold and the bus off time */
240	hostconf = ahc_inb(ahc, HOSTCONF);
241	ahc_outb(ahc, BUSSPD, hostconf & DFTHRSH);
242	ahc_outb(ahc, BUSTIME, (hostconf << 2) & BOFF);
243
244	ahc->bus_softc.aic7770_softc.busspd = hostconf & DFTHRSH;
245	ahc->bus_softc.aic7770_softc.bustime = (hostconf << 2) & BOFF;
246
247	/*
248	 * Generic aic7xxx initialization.
249	 */
250	error = ahc_init(ahc);
251	if (error != 0)
252		return (error);
253
254	error = aic7770_map_int(ahc, irq);
255	if (error != 0)
256		return (error);
257
258	ahc_lock(ahc);
259	/*
260	 * Link this softc in with all other ahc instances.
261	 */
262	ahc_softc_insert(ahc);
263
264	/*
265	 * Enable the board's BUS drivers
266	 */
267	ahc_outb(ahc, BCTL, ENABLE);
268
269	ahc_unlock(ahc);
270
271	return (0);
272}
273
274static int
275aic7770_chip_init(struct ahc_softc *ahc)
276{
277	ahc_outb(ahc, BUSSPD, ahc->bus_softc.aic7770_softc.busspd);
278	ahc_outb(ahc, BUSTIME, ahc->bus_softc.aic7770_softc.bustime);
279	ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~AUTOFLUSHDIS);
280	ahc_outb(ahc, BCTL, ENABLE);
281	return (ahc_chip_init(ahc));
282}
283
284static int
285aic7770_suspend(struct ahc_softc *ahc)
286{
287	return (ahc_suspend(ahc));
288}
289
290static int
291aic7770_resume(struct ahc_softc *ahc)
292{
293	return (ahc_resume(ahc));
294}
295
296/*
297 * Read the 284x SEEPROM.
298 */
299static int
300aha2840_load_seeprom(struct ahc_softc *ahc)
301{
302	struct	seeprom_descriptor sd;
303	struct	seeprom_config *sc;
304	int	have_seeprom;
305	uint8_t scsi_conf;
306
307	sd.sd_ahc = ahc;
308	sd.sd_control_offset = SEECTL_2840;
309	sd.sd_status_offset = STATUS_2840;
310	sd.sd_dataout_offset = STATUS_2840;
311	sd.sd_chip = C46;
312	sd.sd_MS = 0;
313	sd.sd_RDY = EEPROM_TF;
314	sd.sd_CS = CS_2840;
315	sd.sd_CK = CK_2840;
316	sd.sd_DO = DO_2840;
317	sd.sd_DI = DI_2840;
318	sc = ahc->seep_config;
319
320	if (bootverbose)
321		printf("%s: Reading SEEPROM...", ahc_name(ahc));
322	have_seeprom = ahc_read_seeprom(&sd, (uint16_t *)sc,
323					/*start_addr*/0, sizeof(*sc)/2);
324
325	if (have_seeprom) {
326
327		if (ahc_verify_cksum(sc) == 0) {
328			if(bootverbose)
329				printf ("checksum error\n");
330			have_seeprom = 0;
331		} else if (bootverbose) {
332			printf("done.\n");
333		}
334	}
335
336	if (!have_seeprom) {
337		if (bootverbose)
338			printf("%s: No SEEPROM available\n", ahc_name(ahc));
339		ahc->flags |= AHC_USEDEFAULTS;
340	} else {
341		/*
342		 * Put the data we've collected down into SRAM
343		 * where ahc_init will find it.
344		 */
345		int	 i;
346		int	 max_targ;
347		uint16_t discenable;
348
349		max_targ = (ahc->features & AHC_WIDE) != 0 ? 16 : 8;
350		discenable = 0;
351		for (i = 0; i < max_targ; i++){
352			uint8_t target_settings;
353
354			target_settings = (sc->device_flags[i] & CFXFER) << 4;
355			if (sc->device_flags[i] & CFSYNCH)
356				target_settings |= SOFS;
357			if (sc->device_flags[i] & CFWIDEB)
358				target_settings |= WIDEXFER;
359			if (sc->device_flags[i] & CFDISC)
360				discenable |= (0x01 << i);
361			ahc_outb(ahc, TARG_SCSIRATE + i, target_settings);
362		}
363		ahc_outb(ahc, DISC_DSB, ~(discenable & 0xff));
364		ahc_outb(ahc, DISC_DSB + 1, ~((discenable >> 8) & 0xff));
365
366		ahc->our_id = sc->brtime_id & CFSCSIID;
367
368		scsi_conf = (ahc->our_id & 0x7);
369		if (sc->adapter_control & CFSPARITY)
370			scsi_conf |= ENSPCHK;
371		if (sc->adapter_control & CFRESETB)
372			scsi_conf |= RESET_SCSI;
373
374		if (sc->bios_control & CF284XEXTEND)
375			ahc->flags |= AHC_EXTENDED_TRANS_A;
376		/* Set SCSICONF info */
377		ahc_outb(ahc, SCSICONF, scsi_conf);
378
379		if (sc->adapter_control & CF284XSTERM)
380			ahc->flags |= AHC_TERM_ENB_A;
381	}
382	return (have_seeprom);
383}
384
385static int
386ahc_aic7770_VL_setup(struct ahc_softc *ahc)
387{
388	int error;
389
390	error = ahc_aic7770_setup(ahc);
391	ahc->chip |= AHC_VL;
392	return (error);
393}
394
395static int
396ahc_aic7770_EISA_setup(struct ahc_softc *ahc)
397{
398	int error;
399
400	error = ahc_aic7770_setup(ahc);
401	ahc->chip |= AHC_EISA;
402	return (error);
403}
404
405static int
406ahc_aic7770_setup(struct ahc_softc *ahc)
407{
408	ahc->channel = 'A';
409	ahc->channel_b = 'B';
410	ahc->chip = AHC_AIC7770;
411	ahc->features = AHC_AIC7770_FE;
412	ahc->bugs |= AHC_TMODE_WIDEODD_BUG;
413	ahc->flags |= AHC_PAGESCBS;
414	ahc->instruction_ram_size = 448;
415	return (0);
416}
417