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