aic7xxx_seeprom.c revision 1.4.4.1       1  1.4.4.1   nathanw /*	$NetBSD: aic7xxx_seeprom.c,v 1.4.4.1 2001/06/21 20:02:08 nathanw Exp $	*/
      2      1.1   thorpej 
      3      1.2      fvdl /*
      4      1.2      fvdl  * Product specific probe and attach routines for:
      5      1.2      fvdl  *      3940, 2940, aic7895, aic7890, aic7880,
      6      1.2      fvdl  *      aic7870, aic7860 and aic7850 SCSI controllers
      7      1.1   thorpej  *
      8      1.1   thorpej  * These are the SEEPROM-reading functions only.  They were split off from
      9      1.1   thorpej  * the PCI-specific support by Jason R. Thorpe <thorpej (at) netbsd.org>.
     10      1.1   thorpej  *
     11      1.2      fvdl  * Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000 Justin T. Gibbs.
     12      1.1   thorpej  * All rights reserved.
     13      1.1   thorpej  *
     14      1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     15      1.1   thorpej  * modification, are permitted provided that the following conditions
     16      1.1   thorpej  * are met:
     17      1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     18      1.2      fvdl  *    notice, this list of conditions, and the following disclaimer,
     19      1.2      fvdl  *    without modification.
     20      1.2      fvdl  * 2. The name of the author may not be used to endorse or promote products
     21      1.1   thorpej  *    derived from this software without specific prior written permission.
     22      1.1   thorpej  *
     23      1.2      fvdl  * Alternatively, this software may be distributed under the terms of the
     24      1.2      fvdl  * the GNU Public License ("GPL").
     25      1.2      fvdl  *
     26      1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     27      1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28      1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29      1.1   thorpej  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     30      1.1   thorpej  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31      1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32      1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33      1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34      1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35      1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36      1.1   thorpej  * SUCH DAMAGE.
     37      1.1   thorpej  *
     38      1.2      fvdl  * $FreeBSD: src/sys/dev/aic7xxx/ahc_pci.c,v 1.27 2000/01/10 01:47:51 gibbs Exp
     39      1.2      fvdl $
     40      1.2      fvdl  */
     41      1.1   thorpej 
     42      1.1   thorpej #include <sys/param.h>
     43      1.1   thorpej #include <sys/systm.h>
     44      1.1   thorpej #include <sys/malloc.h>
     45      1.1   thorpej #include <sys/kernel.h>
     46      1.1   thorpej #include <sys/queue.h>
     47      1.1   thorpej #include <sys/device.h>
     48      1.4  jdolecek #include <sys/reboot.h>		/* for AB_* needed by bootverbose */
     49      1.1   thorpej 
     50      1.1   thorpej #include <machine/bus.h>
     51      1.1   thorpej #include <machine/intr.h>
     52      1.1   thorpej 
     53      1.1   thorpej #include <dev/scsipi/scsi_all.h>
     54      1.1   thorpej #include <dev/scsipi/scsipi_all.h>
     55      1.1   thorpej #include <dev/scsipi/scsiconf.h>
     56      1.1   thorpej 
     57      1.2      fvdl #include <dev/microcode/aic7xxx/aic7xxx_reg.h>
     58      1.1   thorpej #include <dev/ic/aic7xxxvar.h>
     59      1.1   thorpej #include <dev/ic/smc93cx6var.h>
     60      1.1   thorpej 
     61      1.2      fvdl static void configure_termination(struct ahc_softc *,
     62      1.2      fvdl 				  struct seeprom_descriptor *, u_int, u_int *);
     63      1.1   thorpej 
     64      1.2      fvdl static void ahc_new_term_detect(struct ahc_softc *, int *, int *, int *,
     65      1.2      fvdl 				   int *, int *);
     66      1.2      fvdl static void aic787X_cable_detect(struct ahc_softc *, int *, int *, int *,
     67      1.2      fvdl 				 int *);
     68      1.2      fvdl static void aic785X_cable_detect(struct ahc_softc *, int *, int *, int *);
     69      1.2      fvdl static int acquire_seeprom(struct ahc_softc *, struct seeprom_descriptor *);
     70      1.2      fvdl static void release_seeprom(struct seeprom_descriptor *);
     71      1.2      fvdl static void write_brdctl(struct ahc_softc *, u_int8_t);
     72      1.2      fvdl static u_int8_t read_brdctl(struct ahc_softc *);
     73      1.1   thorpej 
     74      1.1   thorpej /*
     75      1.2      fvdl  * Check the external port logic for a serial eeprom
     76      1.2      fvdl  * and termination/cable detection contrls.
     77      1.1   thorpej  */
     78      1.2      fvdl void
     79      1.2      fvdl check_extport(struct ahc_softc *ahc, u_int *sxfrctl1)
     80      1.2      fvdl {
     81      1.2      fvdl 	struct	  seeprom_descriptor sd;
     82      1.2      fvdl 	struct	  seeprom_config sc;
     83      1.2      fvdl 	u_int	  scsi_conf;
     84      1.2      fvdl 	u_int	  adapter_control;
     85      1.2      fvdl 	int	  have_seeprom;
     86      1.2      fvdl 	int	  have_autoterm;
     87      1.2      fvdl 
     88      1.2      fvdl 	sd.sd_tag = ahc->tag;
     89      1.2      fvdl 	sd.sd_bsh = ahc->bsh;
     90      1.2      fvdl 	sd.sd_control_offset = SEECTL;
     91      1.2      fvdl 	sd.sd_status_offset = SEECTL;
     92      1.2      fvdl 	sd.sd_dataout_offset = SEECTL;
     93      1.1   thorpej 
     94      1.2      fvdl 	/*
     95      1.2      fvdl 	 * For some multi-channel devices, the c46 is simply too
     96      1.2      fvdl 	 * small to work.  For the other controller types, we can
     97      1.2      fvdl 	 * get our information from either SEEPROM type.  Set the
     98      1.2      fvdl 	 * type to start our probe with accordingly.
     99      1.2      fvdl 	 */
    100      1.2      fvdl 	if (ahc->flags & AHC_LARGE_SEEPROM)
    101      1.2      fvdl 		sd.sd_chip = C56_66;
    102      1.2      fvdl 	else
    103      1.2      fvdl 		sd.sd_chip = C46;
    104      1.1   thorpej 
    105      1.1   thorpej 	sd.sd_MS = SEEMS;
    106      1.1   thorpej 	sd.sd_RDY = SEERDY;
    107      1.1   thorpej 	sd.sd_CS = SEECS;
    108      1.1   thorpej 	sd.sd_CK = SEECK;
    109      1.1   thorpej 	sd.sd_DO = SEEDO;
    110      1.1   thorpej 	sd.sd_DI = SEEDI;
    111      1.1   thorpej 
    112      1.2      fvdl 	have_seeprom = acquire_seeprom(ahc, &sd);
    113      1.1   thorpej 	if (have_seeprom) {
    114      1.2      fvdl 
    115      1.2      fvdl 		if (bootverbose)
    116      1.2      fvdl 			printf("%s: Reading SEEPROM...", ahc_name(ahc));
    117      1.2      fvdl 
    118      1.2      fvdl 		for (;;) {
    119      1.2      fvdl 			bus_size_t start_addr;
    120      1.2      fvdl 
    121      1.2      fvdl 			start_addr = 32 * (ahc->channel - 'A');
    122      1.2      fvdl 
    123      1.2      fvdl 			have_seeprom = read_seeprom(&sd, (u_int16_t *)&sc,
    124      1.2      fvdl 						    start_addr, sizeof(sc)/2);
    125      1.2      fvdl 
    126      1.2      fvdl 			if (have_seeprom) {
    127      1.2      fvdl 				/* Check checksum */
    128      1.2      fvdl 				int i;
    129      1.2      fvdl 				int maxaddr;
    130      1.2      fvdl 				u_int32_t checksum;
    131      1.2      fvdl 				u_int16_t *scarray;
    132      1.2      fvdl 
    133      1.2      fvdl 				maxaddr = (sizeof(sc)/2) - 1;
    134      1.2      fvdl 				checksum = 0;
    135      1.2      fvdl 				scarray = (u_int16_t *)≻
    136      1.2      fvdl 
    137      1.2      fvdl 				for (i = 0; i < maxaddr; i++)
    138      1.2      fvdl 					checksum = checksum + scarray[i];
    139      1.2      fvdl 				if (checksum == 0
    140      1.2      fvdl 				 || (checksum & 0xFFFF) != sc.checksum) {
    141      1.2      fvdl 					if (bootverbose && sd.sd_chip == C56_66)
    142      1.2      fvdl 						printf ("checksum error\n");
    143      1.2      fvdl 					have_seeprom = 0;
    144      1.2      fvdl 				} else {
    145      1.2      fvdl 					if (bootverbose)
    146      1.2      fvdl 						printf("done.\n");
    147      1.2      fvdl 					break;
    148      1.2      fvdl 				}
    149      1.1   thorpej 			}
    150      1.2      fvdl 
    151      1.2      fvdl 			if (sd.sd_chip == C56_66)
    152      1.2      fvdl 				break;
    153      1.2      fvdl 			sd.sd_chip = C56_66;
    154      1.1   thorpej 		}
    155      1.1   thorpej 	}
    156      1.2      fvdl 
    157      1.1   thorpej 	if (!have_seeprom) {
    158      1.2      fvdl 		if (bootverbose)
    159      1.3     soren 			printf("%s: No SEEPROM available\n", ahc_name(ahc));
    160      1.1   thorpej 		ahc->flags |= AHC_USEDEFAULTS;
    161      1.2      fvdl 	} else {
    162      1.1   thorpej 		/*
    163      1.1   thorpej 		 * Put the data we've collected down into SRAM
    164      1.1   thorpej 		 * where ahc_init will find it.
    165      1.1   thorpej 		 */
    166      1.1   thorpej 		int i;
    167      1.1   thorpej 		int max_targ = sc.max_targets & CFMAXTARG;
    168      1.2      fvdl 		u_int16_t discenable;
    169      1.2      fvdl 		u_int16_t ultraenb;
    170      1.1   thorpej 
    171      1.2      fvdl 		discenable = 0;
    172      1.2      fvdl 		ultraenb = 0;
    173      1.2      fvdl 		if ((sc.adapter_control & CFULTRAEN) != 0) {
    174      1.2      fvdl 			/*
    175      1.2      fvdl 			 * Determine if this adapter has a "newstyle"
    176      1.2      fvdl 			 * SEEPROM format.
    177      1.2      fvdl 			 */
    178      1.2      fvdl 			for (i = 0; i < max_targ; i++) {
    179      1.2      fvdl 				if ((sc.device_flags[i] & CFSYNCHISULTRA) != 0){
    180      1.2      fvdl 					ahc->flags |= AHC_NEWEEPROM_FMT;
    181      1.2      fvdl 					break;
    182      1.2      fvdl 				}
    183      1.2      fvdl 			}
    184      1.2      fvdl 		}
    185      1.2      fvdl 
    186      1.2      fvdl 		for (i = 0; i < max_targ; i++) {
    187      1.2      fvdl 			u_int     scsirate;
    188      1.2      fvdl 			u_int16_t target_mask;
    189      1.2      fvdl 
    190      1.2      fvdl 			target_mask = 0x01 << i;
    191      1.1   thorpej 			if (sc.device_flags[i] & CFDISC)
    192      1.2      fvdl 				discenable |= target_mask;
    193      1.2      fvdl 			if ((ahc->flags & AHC_NEWEEPROM_FMT) != 0) {
    194      1.2      fvdl 				if ((sc.device_flags[i] & CFSYNCHISULTRA) != 0)
    195      1.2      fvdl 					ultraenb |= target_mask;
    196      1.2      fvdl 			} else if ((sc.adapter_control & CFULTRAEN) != 0) {
    197      1.2      fvdl 				ultraenb |= target_mask;
    198      1.2      fvdl 			}
    199      1.2      fvdl 			if ((sc.device_flags[i] & CFXFER) == 0x04
    200      1.2      fvdl 			 && (ultraenb & target_mask) != 0) {
    201      1.2      fvdl 				/* Treat 10MHz as a non-ultra speed */
    202      1.2      fvdl 				sc.device_flags[i] &= ~CFXFER;
    203      1.2      fvdl 			 	ultraenb &= ~target_mask;
    204      1.2      fvdl 			}
    205      1.2      fvdl 			if ((ahc->features & AHC_ULTRA2) != 0) {
    206      1.2      fvdl 				u_int offset;
    207      1.2      fvdl 
    208      1.2      fvdl 				if (sc.device_flags[i] & CFSYNCH)
    209      1.2      fvdl 					offset = MAX_OFFSET_ULTRA2;
    210      1.2      fvdl 				else
    211      1.2      fvdl 					offset = 0;
    212      1.2      fvdl 				ahc_outb(ahc, TARG_OFFSET + i, offset);
    213      1.2      fvdl 
    214      1.2      fvdl 				scsirate = (sc.device_flags[i] & CFXFER)
    215      1.2      fvdl 					 | ((ultraenb & target_mask)
    216      1.2      fvdl 					    ? 0x8 : 0x0);
    217      1.2      fvdl 				if (sc.device_flags[i] & CFWIDEB)
    218      1.2      fvdl 					scsirate |= WIDEXFER;
    219      1.2      fvdl 			} else {
    220      1.2      fvdl 				scsirate = (sc.device_flags[i] & CFXFER) << 4;
    221      1.2      fvdl 				if (sc.device_flags[i] & CFSYNCH)
    222      1.2      fvdl 					scsirate |= SOFS;
    223      1.2      fvdl 				if (sc.device_flags[i] & CFWIDEB)
    224      1.2      fvdl 					scsirate |= WIDEXFER;
    225      1.2      fvdl 			}
    226      1.2      fvdl 			ahc_outb(ahc, TARG_SCSIRATE + i, scsirate);
    227      1.1   thorpej 		}
    228      1.2      fvdl 		ahc->our_id = sc.brtime_id & CFSCSIID;
    229      1.1   thorpej 
    230      1.2      fvdl 		scsi_conf = (ahc->our_id & 0x7);
    231      1.2      fvdl 		if (sc.adapter_control & CFSPARITY)
    232      1.1   thorpej 			scsi_conf |= ENSPCHK;
    233      1.2      fvdl 		if (sc.adapter_control & CFRESETB)
    234      1.1   thorpej 			scsi_conf |= RESET_SCSI;
    235      1.1   thorpej 
    236      1.2      fvdl 		if (sc.bios_control & CFEXTEND)
    237      1.2      fvdl 			ahc->flags |= AHC_EXTENDED_TRANS_A;
    238      1.2      fvdl 		if (ahc->features & AHC_ULTRA
    239      1.2      fvdl 		 && (ahc->flags & AHC_NEWEEPROM_FMT) == 0) {
    240      1.1   thorpej 			/* Should we enable Ultra mode? */
    241      1.2      fvdl 			if (!(sc.adapter_control & CFULTRAEN))
    242      1.1   thorpej 				/* Treat us as a non-ultra card */
    243      1.2      fvdl 				ultraenb = 0;
    244      1.2      fvdl 		}
    245      1.2      fvdl 		/* Set SCSICONF info */
    246      1.2      fvdl 		ahc_outb(ahc, SCSICONF, scsi_conf);
    247      1.2      fvdl 		ahc_outb(ahc, DISC_DSB, ~(discenable & 0xff));
    248      1.2      fvdl 		ahc_outb(ahc, DISC_DSB + 1, ~((discenable >> 8) & 0xff));
    249      1.2      fvdl 		ahc_outb(ahc, ULTRA_ENB, ultraenb & 0xff);
    250      1.2      fvdl 		ahc_outb(ahc, ULTRA_ENB + 1, (ultraenb >> 8) & 0xff);
    251      1.2      fvdl 	}
    252      1.2      fvdl 
    253      1.2      fvdl 	/*
    254      1.2      fvdl 	 * Cards that have the external logic necessary to talk to
    255      1.2      fvdl 	 * a SEEPROM, are almost certain to have the remaining logic
    256      1.2      fvdl 	 * necessary for auto-termination control.  This assumption
    257      1.2      fvdl 	 * hasn't failed yet...
    258      1.2      fvdl 	 */
    259      1.2      fvdl 	have_autoterm = have_seeprom;
    260      1.2      fvdl 	if (have_seeprom)
    261      1.2      fvdl 		adapter_control = sc.adapter_control;
    262      1.2      fvdl 	else
    263      1.2      fvdl 		adapter_control = CFAUTOTERM;
    264      1.2      fvdl 
    265      1.2      fvdl 	/*
    266      1.2      fvdl 	 * Some low-cost chips have SEEPROM and auto-term control built
    267      1.2      fvdl 	 * in, instead of using a GAL.  They can tell us directly
    268      1.2      fvdl 	 * if the termination logic is enabled.
    269      1.2      fvdl 	 */
    270      1.2      fvdl 	if ((ahc->features & AHC_SPIOCAP) != 0) {
    271      1.2      fvdl 		if ((ahc_inb(ahc, SPIOCAP) & SSPIOCPS) != 0)
    272      1.2      fvdl 			have_autoterm = TRUE;
    273      1.2      fvdl 		else
    274      1.2      fvdl 			have_autoterm = FALSE;
    275      1.2      fvdl 	}
    276      1.2      fvdl 
    277      1.2      fvdl 	if (have_autoterm)
    278      1.2      fvdl 		configure_termination(ahc, &sd, adapter_control, sxfrctl1);
    279      1.2      fvdl 
    280      1.2      fvdl 	release_seeprom(&sd);
    281      1.2      fvdl }
    282      1.2      fvdl 
    283      1.2      fvdl static void
    284      1.2      fvdl configure_termination(struct ahc_softc *ahc,
    285      1.2      fvdl 		      struct seeprom_descriptor *sd,
    286      1.2      fvdl 		      u_int adapter_control,
    287      1.2      fvdl 		      u_int *sxfrctl1)
    288      1.2      fvdl {
    289      1.2      fvdl 	u_int8_t brddat;
    290      1.2      fvdl 
    291      1.2      fvdl 	brddat = 0;
    292      1.2      fvdl 
    293      1.2      fvdl 	/*
    294      1.2      fvdl 	 * Update the settings in sxfrctl1 to match the
    295      1.2      fvdl 	 * termination settings
    296      1.2      fvdl 	 */
    297      1.2      fvdl 	*sxfrctl1 = 0;
    298      1.2      fvdl 
    299      1.2      fvdl 	/*
    300      1.2      fvdl 	 * SEECS must be on for the GALS to latch
    301      1.2      fvdl 	 * the data properly.  Be sure to leave MS
    302      1.2      fvdl 	 * on or we will release the seeprom.
    303      1.2      fvdl 	 */
    304      1.2      fvdl 	SEEPROM_OUTB(sd, sd->sd_MS | sd->sd_CS);
    305      1.2      fvdl 	if ((adapter_control & CFAUTOTERM) != 0
    306      1.2      fvdl 	 || (ahc->features & AHC_NEW_TERMCTL) != 0) {
    307      1.2      fvdl 		int internal50_present;
    308      1.2      fvdl 		int internal68_present;
    309      1.2      fvdl 		int externalcable_present;
    310      1.2      fvdl 		int eeprom_present;
    311      1.2      fvdl 		int enableSEC_low;
    312      1.2      fvdl 		int enableSEC_high;
    313      1.2      fvdl 		int enablePRI_low;
    314      1.2      fvdl 		int enablePRI_high;
    315      1.2      fvdl 
    316      1.2      fvdl 		enableSEC_low = 0;
    317      1.2      fvdl 		enableSEC_high = 0;
    318      1.2      fvdl 		enablePRI_low = 0;
    319      1.2      fvdl 		enablePRI_high = 0;
    320      1.2      fvdl 		if ((ahc->features & AHC_NEW_TERMCTL) != 0) {
    321      1.2      fvdl 			ahc_new_term_detect(ahc, &enableSEC_low,
    322      1.2      fvdl 					       &enableSEC_high,
    323      1.2      fvdl 					       &enablePRI_low,
    324      1.2      fvdl 					       &enablePRI_high,
    325      1.2      fvdl 					       &eeprom_present);
    326      1.2      fvdl 			if ((adapter_control & CFSEAUTOTERM) == 0) {
    327      1.2      fvdl 				if (bootverbose)
    328      1.2      fvdl 					printf("%s: Manual SE Termination\n",
    329      1.2      fvdl 					       ahc_name(ahc));
    330      1.2      fvdl 				enableSEC_low = (adapter_control & CFSTERM);
    331      1.2      fvdl 				enableSEC_high = (adapter_control & CFWSTERM);
    332      1.2      fvdl 			}
    333      1.2      fvdl 			if ((adapter_control & CFAUTOTERM) == 0) {
    334      1.2      fvdl 				if (bootverbose)
    335      1.2      fvdl 					printf("%s: Manual LVD Termination\n",
    336      1.2      fvdl 					       ahc_name(ahc));
    337      1.2      fvdl 				enablePRI_low = enablePRI_high =
    338      1.2      fvdl 				    (adapter_control & CFLVDSTERM);
    339      1.2      fvdl 			}
    340      1.2      fvdl 			/* Make the table calculations below happy */
    341      1.2      fvdl 			internal50_present = 0;
    342      1.2      fvdl 			internal68_present = 1;
    343      1.2      fvdl 			externalcable_present = 1;
    344      1.2      fvdl 		} else if ((ahc->features & AHC_SPIOCAP) != 0) {
    345      1.2      fvdl 			aic785X_cable_detect(ahc, &internal50_present,
    346      1.2      fvdl 					     &externalcable_present,
    347      1.2      fvdl 					     &eeprom_present);
    348      1.2      fvdl 		} else {
    349      1.2      fvdl 			aic787X_cable_detect(ahc, &internal50_present,
    350      1.2      fvdl 					     &internal68_present,
    351      1.2      fvdl 					     &externalcable_present,
    352      1.2      fvdl 					     &eeprom_present);
    353      1.2      fvdl 		}
    354      1.2      fvdl 
    355      1.2      fvdl 		if ((ahc->features & AHC_WIDE) == 0)
    356      1.2      fvdl 			internal68_present = 0;
    357      1.2      fvdl 
    358      1.2      fvdl 		if (bootverbose) {
    359      1.2      fvdl 			if ((ahc->features & AHC_ULTRA2) == 0) {
    360      1.2      fvdl 				printf("%s: internal 50 cable %s present, "
    361      1.2      fvdl 				       "internal 68 cable %s present\n",
    362      1.2      fvdl 				       ahc_name(ahc),
    363      1.2      fvdl 				       internal50_present ? "is":"not",
    364      1.2      fvdl 				       internal68_present ? "is":"not");
    365      1.2      fvdl 
    366      1.2      fvdl 				printf("%s: external cable %s present\n",
    367      1.2      fvdl 				       ahc_name(ahc),
    368      1.2      fvdl 				       externalcable_present ? "is":"not");
    369      1.2      fvdl 			}
    370      1.2      fvdl 			printf("%s: BIOS eeprom %s present\n",
    371      1.2      fvdl 			       ahc_name(ahc), eeprom_present ? "is" : "not");
    372      1.1   thorpej 		}
    373      1.2      fvdl 
    374      1.2      fvdl 		if ((ahc->flags & AHC_INT50_SPEEDFLEX) != 0) {
    375      1.2      fvdl 			/*
    376      1.2      fvdl 			 * The 50 pin connector is a separate bus,
    377      1.2      fvdl 			 * so force it to always be terminated.
    378      1.2      fvdl 			 * In the future, perform current sensing
    379      1.2      fvdl 			 * to determine if we are in the middle of
    380      1.2      fvdl 			 * a properly terminated bus.
    381      1.2      fvdl 			 */
    382      1.2      fvdl 			internal50_present = 0;
    383      1.2      fvdl 		}
    384      1.2      fvdl 
    385      1.2      fvdl 		/*
    386      1.2      fvdl 		 * Now set the termination based on what
    387      1.2      fvdl 		 * we found.
    388      1.2      fvdl 		 * Flash Enable = BRDDAT7
    389      1.2      fvdl 		 * Secondary High Term Enable = BRDDAT6
    390      1.2      fvdl 		 * Secondary Low Term Enable = BRDDAT5 (7890)
    391      1.2      fvdl 		 * Primary High Term Enable = BRDDAT4 (7890)
    392      1.2      fvdl 		 */
    393      1.2      fvdl 		if ((ahc->features & AHC_ULTRA2) == 0
    394      1.2      fvdl 		    && (internal50_present != 0)
    395      1.2      fvdl 		    && (internal68_present != 0)
    396      1.2      fvdl 		    && (externalcable_present != 0)) {
    397      1.2      fvdl 			printf("%s: Illegal cable configuration!!. "
    398      1.2      fvdl 			       "Only two connectors on the "
    399      1.2      fvdl 			       "adapter may be used at a "
    400      1.2      fvdl 			       "time!\n", ahc_name(ahc));
    401      1.2      fvdl 		}
    402      1.2      fvdl 
    403      1.2      fvdl 		if ((ahc->features & AHC_WIDE) != 0
    404      1.2      fvdl 		 && ((externalcable_present == 0)
    405      1.2      fvdl 		  || (internal68_present == 0)
    406      1.2      fvdl 		  || (enableSEC_high != 0))) {
    407      1.2      fvdl 			brddat |= BRDDAT6;
    408      1.2      fvdl 			if (bootverbose) {
    409      1.2      fvdl 				if ((ahc->flags & AHC_INT50_SPEEDFLEX) != 0)
    410      1.2      fvdl 					printf("%s: 68 pin termination "
    411      1.2      fvdl 					       "Enabled\n", ahc_name(ahc));
    412      1.2      fvdl 				else
    413      1.2      fvdl 					printf("%s: %sHigh byte termination "
    414      1.2      fvdl 					       "Enabled\n", ahc_name(ahc),
    415      1.2      fvdl 					       enableSEC_high ? "Secondary "
    416      1.2      fvdl 							      : "");
    417      1.2      fvdl 			}
    418      1.2      fvdl 		}
    419      1.2      fvdl 
    420      1.2      fvdl 		if (((internal50_present ? 1 : 0)
    421      1.2      fvdl 		   + (internal68_present ? 1 : 0)
    422      1.2      fvdl 		   + (externalcable_present ? 1 : 0)) <= 1
    423      1.2      fvdl 		 || (enableSEC_low != 0)) {
    424      1.2      fvdl 			if ((ahc->features & AHC_ULTRA2) != 0)
    425      1.2      fvdl 				brddat |= BRDDAT5;
    426      1.2      fvdl 			else
    427      1.2      fvdl 				*sxfrctl1 |= STPWEN;
    428      1.2      fvdl 			if (bootverbose) {
    429      1.2      fvdl 				if ((ahc->flags & AHC_INT50_SPEEDFLEX) != 0)
    430      1.2      fvdl 					printf("%s: 50 pin termination "
    431      1.2      fvdl 					       "Enabled\n", ahc_name(ahc));
    432      1.2      fvdl 				else
    433      1.2      fvdl 					printf("%s: %sLow byte termination "
    434      1.2      fvdl 					       "Enabled\n", ahc_name(ahc),
    435      1.2      fvdl 					       enableSEC_low ? "Secondary "
    436      1.2      fvdl 							     : "");
    437      1.2      fvdl 			}
    438      1.2      fvdl 		}
    439      1.2      fvdl 
    440      1.2      fvdl 		if (enablePRI_low != 0) {
    441      1.2      fvdl 			*sxfrctl1 |= STPWEN;
    442      1.2      fvdl 			if (bootverbose)
    443      1.2      fvdl 				printf("%s: Primary Low Byte termination "
    444      1.2      fvdl 				       "Enabled\n", ahc_name(ahc));
    445      1.2      fvdl 		}
    446      1.2      fvdl 
    447      1.2      fvdl 		/*
    448      1.2      fvdl 		 * Setup STPWEN before setting up the rest of
    449      1.2      fvdl 		 * the termination per the tech note on the U160 cards.
    450      1.2      fvdl 		 */
    451      1.2      fvdl 		ahc_outb(ahc, SXFRCTL1, *sxfrctl1);
    452      1.2      fvdl 
    453      1.2      fvdl 		if (enablePRI_high != 0) {
    454      1.2      fvdl 			brddat |= BRDDAT4;
    455      1.2      fvdl 			if (bootverbose)
    456      1.2      fvdl 				printf("%s: Primary High Byte "
    457      1.2      fvdl 				       "termination Enabled\n",
    458      1.2      fvdl 				       ahc_name(ahc));
    459      1.2      fvdl 		}
    460      1.2      fvdl 
    461      1.2      fvdl 		write_brdctl(ahc, brddat);
    462      1.2      fvdl 
    463      1.2      fvdl 	} else {
    464      1.2      fvdl 		if ((adapter_control & CFSTERM) != 0) {
    465      1.2      fvdl 			*sxfrctl1 |= STPWEN;
    466      1.2      fvdl 
    467      1.2      fvdl 			if (bootverbose)
    468      1.2      fvdl 				printf("%s: %sLow byte termination Enabled\n",
    469      1.2      fvdl 				       ahc_name(ahc),
    470      1.2      fvdl 				       (ahc->features & AHC_ULTRA2) ? "Primary "
    471      1.2      fvdl 								    : "");
    472      1.2      fvdl 		}
    473      1.2      fvdl 
    474      1.2      fvdl 		if ((adapter_control & CFWSTERM) != 0) {
    475      1.2      fvdl 			brddat |= BRDDAT6;
    476      1.2      fvdl 			if (bootverbose)
    477      1.2      fvdl 				printf("%s: %sHigh byte termination Enabled\n",
    478      1.2      fvdl 				       ahc_name(ahc),
    479      1.2      fvdl 				       (ahc->features & AHC_ULTRA2)
    480      1.2      fvdl 				     ? "Secondary " : "");
    481      1.2      fvdl 		}
    482      1.2      fvdl 
    483      1.2      fvdl 		/*
    484      1.2      fvdl 		 * Setup STPWEN before setting up the rest of
    485      1.2      fvdl 		 * the termination per the tech note on the U160 cards.
    486      1.2      fvdl 		 */
    487      1.2      fvdl 		ahc_outb(ahc, SXFRCTL1, *sxfrctl1);
    488      1.2      fvdl 
    489      1.2      fvdl 		write_brdctl(ahc, brddat);
    490      1.1   thorpej 	}
    491      1.2      fvdl 	SEEPROM_OUTB(sd, sd->sd_MS); /* Clear CS */
    492      1.2      fvdl }
    493      1.2      fvdl 
    494      1.2      fvdl static void
    495      1.2      fvdl ahc_new_term_detect(struct ahc_softc *ahc, int *enableSEC_low,
    496      1.2      fvdl 		    int *enableSEC_high, int *enablePRI_low,
    497      1.2      fvdl 		    int *enablePRI_high, int *eeprom_present)
    498      1.2      fvdl {
    499      1.2      fvdl 	u_int8_t brdctl;
    500      1.2      fvdl 
    501      1.2      fvdl 	/*
    502      1.2      fvdl 	 * BRDDAT7 = Eeprom
    503      1.2      fvdl 	 * BRDDAT6 = Enable Secondary High Byte termination
    504      1.2      fvdl 	 * BRDDAT5 = Enable Secondary Low Byte termination
    505      1.2      fvdl 	 * BRDDAT4 = Enable Primary high byte termination
    506      1.2      fvdl 	 * BRDDAT3 = Enable Primary low byte termination
    507      1.2      fvdl 	 */
    508      1.2      fvdl 	brdctl = read_brdctl(ahc);
    509      1.2      fvdl 	*eeprom_present = brdctl & BRDDAT7;
    510      1.2      fvdl 	*enableSEC_high = (brdctl & BRDDAT6);
    511      1.2      fvdl 	*enableSEC_low = (brdctl & BRDDAT5);
    512      1.2      fvdl 	*enablePRI_high = (brdctl & BRDDAT4);
    513      1.2      fvdl 	*enablePRI_low = (brdctl & BRDDAT3);
    514      1.2      fvdl }
    515      1.2      fvdl 
    516      1.2      fvdl static void
    517      1.2      fvdl aic787X_cable_detect(struct ahc_softc *ahc, int *internal50_present,
    518      1.2      fvdl 		     int *internal68_present, int *externalcable_present,
    519      1.2      fvdl 		     int *eeprom_present)
    520      1.2      fvdl {
    521      1.2      fvdl 	u_int8_t brdctl;
    522      1.2      fvdl 
    523      1.2      fvdl 	/*
    524      1.2      fvdl 	 * First read the status of our cables.
    525      1.2      fvdl 	 * Set the rom bank to 0 since the
    526      1.2      fvdl 	 * bank setting serves as a multiplexor
    527      1.2      fvdl 	 * for the cable detection logic.
    528      1.2      fvdl 	 * BRDDAT5 controls the bank switch.
    529      1.2      fvdl 	 */
    530      1.2      fvdl 	write_brdctl(ahc, 0);
    531      1.2      fvdl 
    532      1.2      fvdl 	/*
    533      1.2      fvdl 	 * Now read the state of the internal
    534      1.2      fvdl 	 * connectors.  BRDDAT6 is INT50 and
    535      1.2      fvdl 	 * BRDDAT7 is INT68.
    536      1.2      fvdl 	 */
    537      1.2      fvdl 	brdctl = read_brdctl(ahc);
    538      1.2      fvdl 	*internal50_present = !(brdctl & BRDDAT6);
    539      1.2      fvdl 	*internal68_present = !(brdctl & BRDDAT7);
    540      1.2      fvdl 
    541      1.2      fvdl 	/*
    542      1.2      fvdl 	 * Set the rom bank to 1 and determine
    543      1.2      fvdl 	 * the other signals.
    544      1.2      fvdl 	 */
    545      1.2      fvdl 	write_brdctl(ahc, BRDDAT5);
    546      1.2      fvdl 
    547      1.2      fvdl 	/*
    548      1.2      fvdl 	 * Now read the state of the external
    549      1.2      fvdl 	 * connectors.  BRDDAT6 is EXT68 and
    550      1.2      fvdl 	 * BRDDAT7 is EPROMPS.
    551      1.2      fvdl 	 */
    552      1.2      fvdl 	brdctl = read_brdctl(ahc);
    553      1.2      fvdl 	*externalcable_present = !(brdctl & BRDDAT6);
    554      1.2      fvdl 	*eeprom_present = brdctl & BRDDAT7;
    555      1.1   thorpej }
    556      1.1   thorpej 
    557      1.2      fvdl static void
    558      1.2      fvdl aic785X_cable_detect(struct ahc_softc *ahc, int *internal50_present,
    559      1.2      fvdl 		     int *externalcable_present, int *eeprom_present)
    560      1.2      fvdl {
    561      1.2      fvdl 	u_int8_t brdctl;
    562      1.2      fvdl 
    563      1.2      fvdl 	ahc_outb(ahc, BRDCTL, BRDRW|BRDCS);
    564      1.2      fvdl 	ahc_outb(ahc, BRDCTL, 0);
    565      1.2      fvdl 	brdctl = ahc_inb(ahc, BRDCTL);
    566      1.2      fvdl 	*internal50_present = !(brdctl & BRDDAT5);
    567      1.2      fvdl 	*externalcable_present = !(brdctl & BRDDAT6);
    568      1.2      fvdl 
    569      1.2      fvdl 	*eeprom_present = (ahc_inb(ahc, SPIOCAP) & EEPROM) != 0;
    570      1.2      fvdl }
    571      1.2      fvdl 
    572      1.2      fvdl static int
    573      1.2      fvdl acquire_seeprom(struct ahc_softc *ahc, struct seeprom_descriptor *sd)
    574      1.1   thorpej {
    575      1.1   thorpej 	int wait;
    576      1.1   thorpej 
    577      1.2      fvdl 	if ((ahc->features & AHC_SPIOCAP) != 0
    578      1.2      fvdl 	 && (ahc_inb(ahc, SPIOCAP) & SEEPROM) == 0)
    579      1.2      fvdl 		return (0);
    580      1.2      fvdl 
    581      1.1   thorpej 	/*
    582      1.1   thorpej 	 * Request access of the memory port.  When access is
    583      1.3     soren 	 * granted, SEERDY will go high.  We use a 100 msec
    584      1.3     soren 	 * timeout which should be near 100 msecs more than
    585      1.1   thorpej 	 * is needed.  Reason: after the chip reset, there
    586      1.1   thorpej 	 * should be no contention.
    587      1.1   thorpej 	 */
    588      1.1   thorpej 	SEEPROM_OUTB(sd, sd->sd_MS);
    589      1.3     soren 	wait = 100;  /* 100 msec timeout */
    590      1.2      fvdl 	while (--wait && ((SEEPROM_STATUS_INB(sd) & sd->sd_RDY) == 0)) {
    591      1.2      fvdl 		DELAY(1000);  /* delay 1 msec */
    592      1.2      fvdl 	}
    593      1.2      fvdl 	if ((SEEPROM_STATUS_INB(sd) & sd->sd_RDY) == 0) {
    594      1.1   thorpej 		SEEPROM_OUTB(sd, 0);
    595      1.1   thorpej 		return (0);
    596      1.2      fvdl 	}
    597      1.1   thorpej 	return(1);
    598      1.1   thorpej }
    599      1.1   thorpej 
    600      1.2      fvdl static void
    601  1.4.4.1   nathanw release_seeprom(struct seeprom_descriptor *sd)
    602      1.1   thorpej {
    603      1.1   thorpej 	/* Release access to the memory port and the serial EEPROM. */
    604      1.1   thorpej 	SEEPROM_OUTB(sd, 0);
    605      1.2      fvdl }
    606      1.2      fvdl 
    607      1.2      fvdl static void
    608  1.4.4.1   nathanw write_brdctl(struct ahc_softc *ahc, u_int8_t value)
    609      1.2      fvdl {
    610      1.2      fvdl 	u_int8_t brdctl;
    611      1.2      fvdl 
    612      1.2      fvdl 	if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7895) {
    613      1.2      fvdl 		brdctl = BRDSTB;
    614      1.2      fvdl 	 	if (ahc->channel == 'B')
    615      1.2      fvdl 			brdctl |= BRDCS;
    616      1.2      fvdl 	} else if ((ahc->features & AHC_ULTRA2) != 0) {
    617      1.2      fvdl 		brdctl = 0;
    618      1.2      fvdl 	} else {
    619      1.2      fvdl 		brdctl = BRDSTB|BRDCS;
    620      1.2      fvdl 	}
    621      1.2      fvdl 	ahc_outb(ahc, BRDCTL, brdctl);
    622      1.2      fvdl 	DELAY(20);
    623      1.2      fvdl 	brdctl |= value;
    624      1.2      fvdl 	ahc_outb(ahc, BRDCTL, brdctl);
    625      1.2      fvdl 	DELAY(20);
    626      1.2      fvdl 	if ((ahc->features & AHC_ULTRA2) != 0)
    627      1.2      fvdl 		brdctl |= BRDSTB_ULTRA2;
    628      1.2      fvdl 	else
    629      1.2      fvdl 		brdctl &= ~BRDSTB;
    630      1.2      fvdl 	ahc_outb(ahc, BRDCTL, brdctl);
    631      1.2      fvdl 	DELAY(20);
    632      1.2      fvdl 	if ((ahc->features & AHC_ULTRA2) != 0)
    633      1.2      fvdl 		brdctl = 0;
    634      1.2      fvdl 	else
    635      1.2      fvdl 		brdctl &= ~BRDCS;
    636      1.2      fvdl 	ahc_outb(ahc, BRDCTL, brdctl);
    637      1.2      fvdl }
    638      1.2      fvdl 
    639      1.2      fvdl static u_int8_t
    640  1.4.4.1   nathanw read_brdctl(struct ahc_softc *ahc)
    641      1.2      fvdl {
    642      1.2      fvdl 	u_int8_t brdctl;
    643      1.2      fvdl 	u_int8_t value;
    644      1.2      fvdl 
    645      1.2      fvdl 	if ((ahc->chip & AHC_CHIPID_MASK) == AHC_AIC7895) {
    646      1.2      fvdl 		brdctl = BRDRW;
    647      1.2      fvdl 	 	if (ahc->channel == 'B')
    648      1.2      fvdl 			brdctl |= BRDCS;
    649      1.2      fvdl 	} else if ((ahc->features & AHC_ULTRA2) != 0) {
    650      1.2      fvdl 		brdctl = BRDRW_ULTRA2;
    651      1.2      fvdl 	} else {
    652      1.2      fvdl 		brdctl = BRDRW|BRDCS;
    653      1.2      fvdl 	}
    654      1.2      fvdl 	ahc_outb(ahc, BRDCTL, brdctl);
    655      1.2      fvdl 	DELAY(20);
    656      1.2      fvdl 	value = ahc_inb(ahc, BRDCTL);
    657      1.2      fvdl 	ahc_outb(ahc, BRDCTL, 0);
    658      1.2      fvdl 	return (value);
    659      1.1   thorpej }
    660