1139749Simp/*-
265942Sgibbs * Product specific probe and attach routines for:
365942Sgibbs * 	27/284X and aic7770 motherboard SCSI controllers
465942Sgibbs *
571717Sgibbs * Copyright (c) 1994-1998, 2000, 2001 Justin T. Gibbs.
665942Sgibbs * All rights reserved.
765942Sgibbs *
865942Sgibbs * Redistribution and use in source and binary forms, with or without
965942Sgibbs * modification, are permitted provided that the following conditions
1065942Sgibbs * are met:
1165942Sgibbs * 1. Redistributions of source code must retain the above copyright
1295378Sgibbs *    notice, this list of conditions, and the following disclaimer,
1395378Sgibbs *    without modification.
1495378Sgibbs * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1595378Sgibbs *    substantially similar to the "NO WARRANTY" disclaimer below
1695378Sgibbs *    ("Disclaimer") and any redistribution must be conditioned upon
1795378Sgibbs *    including a substantially similar Disclaimer requirement for further
1895378Sgibbs *    binary redistribution.
1995378Sgibbs * 3. Neither the names of the above-listed copyright holders nor the names
2095378Sgibbs *    of any contributors may be used to endorse or promote products derived
2195378Sgibbs *    from this software without specific prior written permission.
2265942Sgibbs *
2371717Sgibbs * Alternatively, this software may be distributed under the terms of the
2495378Sgibbs * GNU General Public License ("GPL") version 2 as published by the Free
2595378Sgibbs * Software Foundation.
2671717Sgibbs *
2795378Sgibbs * NO WARRANTY
2895378Sgibbs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2995378Sgibbs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3095378Sgibbs * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
3195378Sgibbs * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3295378Sgibbs * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3365942Sgibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3465942Sgibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3595378Sgibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3695378Sgibbs * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
3795378Sgibbs * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3895378Sgibbs * POSSIBILITY OF SUCH DAMAGES.
3965942Sgibbs *
40123579Sgibbs * $Id: //depot/aic7xxx/aic7xxx/aic7770.c#34 $
4165942Sgibbs */
4265942Sgibbs
4395378Sgibbs#ifdef __linux__
4495378Sgibbs#include "aic7xxx_osm.h"
4595378Sgibbs#include "aic7xxx_inline.h"
4695378Sgibbs#include "aic7xxx_93cx6.h"
4795378Sgibbs#else
48123579Sgibbs#include <sys/cdefs.h>
49123579Sgibbs__FBSDID("$FreeBSD$");
5095378Sgibbs#include <dev/aic7xxx/aic7xxx_osm.h>
5165942Sgibbs#include <dev/aic7xxx/aic7xxx_inline.h>
5265942Sgibbs#include <dev/aic7xxx/aic7xxx_93cx6.h>
5395378Sgibbs#endif
5465942Sgibbs
5565942Sgibbs#define ID_AIC7770	0x04907770
5665942Sgibbs#define ID_AHA_274x	0x04907771
5765942Sgibbs#define ID_AHA_284xB	0x04907756 /* BIOS enabled */
5865942Sgibbs#define ID_AHA_284x	0x04907757 /* BIOS disabled*/
59107411Sscottl#define	ID_OLV_274x	0x04907782 /* Olivetti OEM */
60107411Sscottl#define	ID_OLV_274xD	0x04907783 /* Olivetti OEM (Differential) */
6165942Sgibbs
62114621Sgibbsstatic int aic7770_chip_init(struct ahc_softc *ahc);
63114621Sgibbsstatic int aic7770_suspend(struct ahc_softc *ahc);
64114621Sgibbsstatic int aic7770_resume(struct ahc_softc *ahc);
65102669Sgibbsstatic int aha2840_load_seeprom(struct ahc_softc *ahc);
6665942Sgibbsstatic ahc_device_setup_t ahc_aic7770_VL_setup;
67132107Sstefanfstatic ahc_device_setup_t ahc_aic7770_EISA_setup;
6865942Sgibbsstatic ahc_device_setup_t ahc_aic7770_setup;
6965942Sgibbs
70115915Sgibbsstruct aic7770_identity aic7770_ident_table[] =
7165942Sgibbs{
7265942Sgibbs	{
7365942Sgibbs		ID_AHA_274x,
7465942Sgibbs		0xFFFFFFFF,
7565942Sgibbs		"Adaptec 274X SCSI adapter",
7665942Sgibbs		ahc_aic7770_EISA_setup
7765942Sgibbs	},
7865942Sgibbs	{
7965942Sgibbs		ID_AHA_284xB,
8065942Sgibbs		0xFFFFFFFE,
8165942Sgibbs		"Adaptec 284X SCSI adapter",
8265942Sgibbs		ahc_aic7770_VL_setup
8365942Sgibbs	},
84107411Sscottl	{
85115915Sgibbs		ID_AHA_284x,
86115915Sgibbs		0xFFFFFFFE,
87115915Sgibbs		"Adaptec 284X SCSI adapter (BIOS Disabled)",
88115915Sgibbs		ahc_aic7770_VL_setup
89115915Sgibbs	},
90115915Sgibbs	{
91107411Sscottl		ID_OLV_274x,
92107411Sscottl		0xFFFFFFFF,
93107411Sscottl		"Adaptec (Olivetti OEM) 274X SCSI adapter",
94107411Sscottl		ahc_aic7770_EISA_setup
95107411Sscottl	},
96107411Sscottl	{
97107411Sscottl		ID_OLV_274xD,
98107411Sscottl		0xFFFFFFFF,
99107411Sscottl		"Adaptec (Olivetti OEM) 274X Differential SCSI adapter",
100107411Sscottl		ahc_aic7770_EISA_setup
101107411Sscottl	},
10265942Sgibbs	/* Generic chip probes for devices we don't know 'exactly' */
10365942Sgibbs	{
10465942Sgibbs		ID_AIC7770,
10565942Sgibbs		0xFFFFFFFF,
10665942Sgibbs		"Adaptec aic7770 SCSI adapter",
10765942Sgibbs		ahc_aic7770_EISA_setup
10865942Sgibbs	}
10965942Sgibbs};
11065942Sgibbsconst int ahc_num_aic7770_devs = NUM_ELEMENTS(aic7770_ident_table);
11165942Sgibbs
11265942Sgibbsstruct aic7770_identity *
11365942Sgibbsaic7770_find_device(uint32_t id)
11465942Sgibbs{
11565942Sgibbs	struct	aic7770_identity *entry;
11665942Sgibbs	int	i;
11765942Sgibbs
11865942Sgibbs	for (i = 0; i < ahc_num_aic7770_devs; i++) {
11965942Sgibbs		entry = &aic7770_ident_table[i];
12065942Sgibbs		if (entry->full_id == (id & entry->id_mask))
12165942Sgibbs			return (entry);
12265942Sgibbs	}
12365942Sgibbs	return (NULL);
12465942Sgibbs}
12565942Sgibbs
12665942Sgibbsint
12795378Sgibbsaic7770_config(struct ahc_softc *ahc, struct aic7770_identity *entry, u_int io)
12865942Sgibbs{
12965942Sgibbs	int	error;
130102669Sgibbs	int	have_seeprom;
13165942Sgibbs	u_int	hostconf;
13274094Sgibbs	u_int   irq;
13374094Sgibbs	u_int	intdef;
13465942Sgibbs
13579874Sgibbs	error = entry->setup(ahc);
136102669Sgibbs	have_seeprom = 0;
13765942Sgibbs	if (error != 0)
13865942Sgibbs		return (error);
13965942Sgibbs
14095378Sgibbs	error = aic7770_map_registers(ahc, io);
14165942Sgibbs	if (error != 0)
14265942Sgibbs		return (error);
14365942Sgibbs
14495378Sgibbs	/*
14595378Sgibbs	 * Before we continue probing the card, ensure that
14695378Sgibbs	 * its interrupts are *disabled*.  We don't want
14795378Sgibbs	 * a misstep to hang the machine in an interrupt
14895378Sgibbs	 * storm.
14995378Sgibbs	 */
15095378Sgibbs	ahc_intr_enable(ahc, FALSE);
15195378Sgibbs
15279874Sgibbs	ahc->description = entry->name;
15379874Sgibbs	error = ahc_softc_init(ahc);
154114621Sgibbs	if (error != 0)
155114621Sgibbs		return (error);
15665942Sgibbs
157114621Sgibbs	ahc->bus_chip_init = aic7770_chip_init;
158114621Sgibbs	ahc->bus_suspend = aic7770_suspend;
159114621Sgibbs	ahc->bus_resume = aic7770_resume;
160114621Sgibbs
161115917Sgibbs	error = ahc_reset(ahc, /*reinit*/FALSE);
16265942Sgibbs	if (error != 0)
16365942Sgibbs		return (error);
16465942Sgibbs
16574094Sgibbs	/* Make sure we have a valid interrupt vector */
16674094Sgibbs	intdef = ahc_inb(ahc, INTDEF);
16774094Sgibbs	irq = intdef & VECTOR;
16874094Sgibbs	switch (irq) {
16974094Sgibbs	case 9:
17074094Sgibbs	case 10:
17174094Sgibbs	case 11:
17274094Sgibbs	case 12:
17374094Sgibbs	case 14:
17474094Sgibbs	case 15:
17574094Sgibbs		break;
17674094Sgibbs	default:
177123579Sgibbs		printf("aic7770_config: invalid irq setting %d\n", intdef);
17874094Sgibbs		return (ENXIO);
17974094Sgibbs	}
18074094Sgibbs
18174094Sgibbs	if ((intdef & EDGE_TRIG) != 0)
18274094Sgibbs		ahc->flags |= AHC_EDGE_INTERRUPT;
18374094Sgibbs
18479874Sgibbs	switch (ahc->chip & (AHC_EISA|AHC_VL)) {
18565942Sgibbs	case AHC_EISA:
18665942Sgibbs	{
18765942Sgibbs		u_int biosctrl;
18865942Sgibbs		u_int scsiconf;
18965942Sgibbs		u_int scsiconf1;
19065942Sgibbs
19165942Sgibbs		biosctrl = ahc_inb(ahc, HA_274_BIOSCTRL);
19265942Sgibbs		scsiconf = ahc_inb(ahc, SCSICONF);
19365942Sgibbs		scsiconf1 = ahc_inb(ahc, SCSICONF + 1);
19465942Sgibbs
19565942Sgibbs		/* Get the primary channel information */
19665942Sgibbs		if ((biosctrl & CHANNEL_B_PRIMARY) != 0)
19774094Sgibbs			ahc->flags |= 1;
19865942Sgibbs
19965942Sgibbs		if ((biosctrl & BIOSMODE) == BIOSDISABLED) {
20065942Sgibbs			ahc->flags |= AHC_USEDEFAULTS;
20165942Sgibbs		} else {
20265942Sgibbs			if ((ahc->features & AHC_WIDE) != 0) {
20365942Sgibbs				ahc->our_id = scsiconf1 & HWSCSIID;
20465942Sgibbs				if (scsiconf & TERM_ENB)
20565942Sgibbs					ahc->flags |= AHC_TERM_ENB_A;
20665942Sgibbs			} else {
20765942Sgibbs				ahc->our_id = scsiconf & HSCSIID;
20865942Sgibbs				ahc->our_id_b = scsiconf1 & HSCSIID;
20965942Sgibbs				if (scsiconf & TERM_ENB)
21065942Sgibbs					ahc->flags |= AHC_TERM_ENB_A;
21165942Sgibbs				if (scsiconf1 & TERM_ENB)
21265942Sgibbs					ahc->flags |= AHC_TERM_ENB_B;
21365942Sgibbs			}
21465942Sgibbs		}
215102669Sgibbs		if ((ahc_inb(ahc, HA_274_BIOSGLOBAL) & HA_274_EXTENDED_TRANS))
216102669Sgibbs			ahc->flags |= AHC_EXTENDED_TRANS_A|AHC_EXTENDED_TRANS_B;
21765942Sgibbs		break;
21865942Sgibbs	}
21965942Sgibbs	case AHC_VL:
22065942Sgibbs	{
221102669Sgibbs		have_seeprom = aha2840_load_seeprom(ahc);
22265942Sgibbs		break;
22365942Sgibbs	}
22465942Sgibbs	default:
22565942Sgibbs		break;
22665942Sgibbs	}
227102669Sgibbs	if (have_seeprom == 0) {
228102669Sgibbs		free(ahc->seep_config, M_DEVBUF);
229102669Sgibbs		ahc->seep_config = NULL;
230102669Sgibbs	}
23165942Sgibbs
23265942Sgibbs	/*
23365942Sgibbs	 * Ensure autoflush is enabled
23465942Sgibbs	 */
23565942Sgibbs	ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~AUTOFLUSHDIS);
23665942Sgibbs
23765942Sgibbs	/* Setup the FIFO threshold and the bus off time */
23865942Sgibbs	hostconf = ahc_inb(ahc, HOSTCONF);
23965942Sgibbs	ahc_outb(ahc, BUSSPD, hostconf & DFTHRSH);
24065942Sgibbs	ahc_outb(ahc, BUSTIME, (hostconf << 2) & BOFF);
24165942Sgibbs
242114621Sgibbs	ahc->bus_softc.aic7770_softc.busspd = hostconf & DFTHRSH;
243114621Sgibbs	ahc->bus_softc.aic7770_softc.bustime = (hostconf << 2) & BOFF;
244114621Sgibbs
24565942Sgibbs	/*
24665942Sgibbs	 * Generic aic7xxx initialization.
24765942Sgibbs	 */
24865942Sgibbs	error = ahc_init(ahc);
24965942Sgibbs	if (error != 0)
25065942Sgibbs		return (error);
25165942Sgibbs
252102669Sgibbs	error = aic7770_map_int(ahc, irq);
253102669Sgibbs	if (error != 0)
254102669Sgibbs		return (error);
255102669Sgibbs
256168807Sscottl	ahc_lock(ahc);
25765942Sgibbs	/*
25871390Sgibbs	 * Link this softc in with all other ahc instances.
25971390Sgibbs	 */
26071390Sgibbs	ahc_softc_insert(ahc);
26171390Sgibbs
26271390Sgibbs	/*
26365942Sgibbs	 * Enable the board's BUS drivers
26465942Sgibbs	 */
26565942Sgibbs	ahc_outb(ahc, BCTL, ENABLE);
26665942Sgibbs
267168807Sscottl	ahc_unlock(ahc);
26874972Sgibbs
26965942Sgibbs	return (0);
27065942Sgibbs}
27165942Sgibbs
272114621Sgibbsstatic int
273114621Sgibbsaic7770_chip_init(struct ahc_softc *ahc)
274114621Sgibbs{
275114621Sgibbs	ahc_outb(ahc, BUSSPD, ahc->bus_softc.aic7770_softc.busspd);
276114621Sgibbs	ahc_outb(ahc, BUSTIME, ahc->bus_softc.aic7770_softc.bustime);
277114621Sgibbs	ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) & ~AUTOFLUSHDIS);
278114621Sgibbs	ahc_outb(ahc, BCTL, ENABLE);
279114621Sgibbs	return (ahc_chip_init(ahc));
280114621Sgibbs}
281114621Sgibbs
282114621Sgibbsstatic int
283114621Sgibbsaic7770_suspend(struct ahc_softc *ahc)
284114621Sgibbs{
285114621Sgibbs	return (ahc_suspend(ahc));
286114621Sgibbs}
287114621Sgibbs
288114621Sgibbsstatic int
289114621Sgibbsaic7770_resume(struct ahc_softc *ahc)
290114621Sgibbs{
291114621Sgibbs	return (ahc_resume(ahc));
292114621Sgibbs}
293114621Sgibbs
29465942Sgibbs/*
29565942Sgibbs * Read the 284x SEEPROM.
29665942Sgibbs */
297102669Sgibbsstatic int
29865942Sgibbsaha2840_load_seeprom(struct ahc_softc *ahc)
29965942Sgibbs{
300102669Sgibbs	struct	seeprom_descriptor sd;
301102669Sgibbs	struct	seeprom_config *sc;
302102669Sgibbs	int	have_seeprom;
303102669Sgibbs	uint8_t scsi_conf;
30465942Sgibbs
30565942Sgibbs	sd.sd_ahc = ahc;
30665942Sgibbs	sd.sd_control_offset = SEECTL_2840;
30765942Sgibbs	sd.sd_status_offset = STATUS_2840;
30865942Sgibbs	sd.sd_dataout_offset = STATUS_2840;
30965942Sgibbs	sd.sd_chip = C46;
31065942Sgibbs	sd.sd_MS = 0;
31165942Sgibbs	sd.sd_RDY = EEPROM_TF;
31265942Sgibbs	sd.sd_CS = CS_2840;
31365942Sgibbs	sd.sd_CK = CK_2840;
31465942Sgibbs	sd.sd_DO = DO_2840;
31565942Sgibbs	sd.sd_DI = DI_2840;
316102669Sgibbs	sc = ahc->seep_config;
31765942Sgibbs
31865942Sgibbs	if (bootverbose)
31965942Sgibbs		printf("%s: Reading SEEPROM...", ahc_name(ahc));
320104231Sgibbs	have_seeprom = ahc_read_seeprom(&sd, (uint16_t *)sc,
321114621Sgibbs					/*start_addr*/0, sizeof(*sc)/2);
32265942Sgibbs
32365942Sgibbs	if (have_seeprom) {
32465942Sgibbs
325102669Sgibbs		if (ahc_verify_cksum(sc) == 0) {
32665942Sgibbs			if(bootverbose)
32765942Sgibbs				printf ("checksum error\n");
32865942Sgibbs			have_seeprom = 0;
32965942Sgibbs		} else if (bootverbose) {
33065942Sgibbs			printf("done.\n");
33165942Sgibbs		}
33265942Sgibbs	}
33365942Sgibbs
33465942Sgibbs	if (!have_seeprom) {
33565942Sgibbs		if (bootverbose)
33665942Sgibbs			printf("%s: No SEEPROM available\n", ahc_name(ahc));
33765942Sgibbs		ahc->flags |= AHC_USEDEFAULTS;
33865942Sgibbs	} else {
33965942Sgibbs		/*
34065942Sgibbs		 * Put the data we've collected down into SRAM
34165942Sgibbs		 * where ahc_init will find it.
34265942Sgibbs		 */
343102669Sgibbs		int	 i;
344102669Sgibbs		int	 max_targ;
34565942Sgibbs		uint16_t discenable;
34665942Sgibbs
347102669Sgibbs		max_targ = (ahc->features & AHC_WIDE) != 0 ? 16 : 8;
34865942Sgibbs		discenable = 0;
34965942Sgibbs		for (i = 0; i < max_targ; i++){
350102669Sgibbs			uint8_t target_settings;
351102669Sgibbs
352102669Sgibbs			target_settings = (sc->device_flags[i] & CFXFER) << 4;
353102669Sgibbs			if (sc->device_flags[i] & CFSYNCH)
35465942Sgibbs				target_settings |= SOFS;
355102669Sgibbs			if (sc->device_flags[i] & CFWIDEB)
35665942Sgibbs				target_settings |= WIDEXFER;
357102669Sgibbs			if (sc->device_flags[i] & CFDISC)
35865942Sgibbs				discenable |= (0x01 << i);
35965942Sgibbs			ahc_outb(ahc, TARG_SCSIRATE + i, target_settings);
36065942Sgibbs		}
36165942Sgibbs		ahc_outb(ahc, DISC_DSB, ~(discenable & 0xff));
36265942Sgibbs		ahc_outb(ahc, DISC_DSB + 1, ~((discenable >> 8) & 0xff));
36365942Sgibbs
364102669Sgibbs		ahc->our_id = sc->brtime_id & CFSCSIID;
36565942Sgibbs
36665942Sgibbs		scsi_conf = (ahc->our_id & 0x7);
367102669Sgibbs		if (sc->adapter_control & CFSPARITY)
36865942Sgibbs			scsi_conf |= ENSPCHK;
369102669Sgibbs		if (sc->adapter_control & CFRESETB)
37065942Sgibbs			scsi_conf |= RESET_SCSI;
37165942Sgibbs
372102669Sgibbs		if (sc->bios_control & CF284XEXTEND)
37365942Sgibbs			ahc->flags |= AHC_EXTENDED_TRANS_A;
37465942Sgibbs		/* Set SCSICONF info */
37565942Sgibbs		ahc_outb(ahc, SCSICONF, scsi_conf);
37665942Sgibbs
377102669Sgibbs		if (sc->adapter_control & CF284XSTERM)
37865942Sgibbs			ahc->flags |= AHC_TERM_ENB_A;
37965942Sgibbs	}
380102669Sgibbs	return (have_seeprom);
38165942Sgibbs}
38265942Sgibbs
38365942Sgibbsstatic int
38479874Sgibbsahc_aic7770_VL_setup(struct ahc_softc *ahc)
38565942Sgibbs{
38665942Sgibbs	int error;
38765942Sgibbs
38879874Sgibbs	error = ahc_aic7770_setup(ahc);
38979874Sgibbs	ahc->chip |= AHC_VL;
39065942Sgibbs	return (error);
39165942Sgibbs}
39265942Sgibbs
39365942Sgibbsstatic int
39479874Sgibbsahc_aic7770_EISA_setup(struct ahc_softc *ahc)
39565942Sgibbs{
39665942Sgibbs	int error;
39765942Sgibbs
39879874Sgibbs	error = ahc_aic7770_setup(ahc);
39979874Sgibbs	ahc->chip |= AHC_EISA;
40065942Sgibbs	return (error);
40165942Sgibbs}
40265942Sgibbs
40365942Sgibbsstatic int
40479874Sgibbsahc_aic7770_setup(struct ahc_softc *ahc)
40565942Sgibbs{
40679874Sgibbs	ahc->channel = 'A';
40779874Sgibbs	ahc->channel_b = 'B';
40879874Sgibbs	ahc->chip = AHC_AIC7770;
40979874Sgibbs	ahc->features = AHC_AIC7770_FE;
41079874Sgibbs	ahc->bugs |= AHC_TMODE_WIDEODD_BUG;
41179874Sgibbs	ahc->flags |= AHC_PAGESCBS;
412114621Sgibbs	ahc->instruction_ram_size = 448;
41365942Sgibbs	return (0);
41465942Sgibbs}
415