Home | History | Annotate | Line # | Download | only in ic
aic7xxx_seeprom.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: aic7xxx_seeprom.c,v 1.1 2000/01/26 06:04:40 thorpej Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Product specific probe and attach routines for:
      5  1.1  thorpej  *      3940, 2940, aic7880, aic7870, aic7860 and aic7850 SCSI controllers
      6  1.1  thorpej  *
      7  1.1  thorpej  * These are the SEEPROM-reading functions only.  They were split off from
      8  1.1  thorpej  * the PCI-specific support by Jason R. Thorpe <thorpej (at) netbsd.org>.
      9  1.1  thorpej  *
     10  1.1  thorpej  * Copyright (c) 1995, 1996 Justin T. Gibbs.
     11  1.1  thorpej  * All rights reserved.
     12  1.1  thorpej  *
     13  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     14  1.1  thorpej  * modification, are permitted provided that the following conditions
     15  1.1  thorpej  * are met:
     16  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     17  1.1  thorpej  *    notice immediately at the beginning of the file, without modification,
     18  1.1  thorpej  *    this list of conditions, and the following disclaimer.
     19  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     20  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     21  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     22  1.1  thorpej  * 3. The name of the author may not be used to endorse or promote products
     23  1.1  thorpej  *    derived from this software without specific prior written permission.
     24  1.1  thorpej  *
     25  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  thorpej  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     29  1.1  thorpej  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  thorpej  * SUCH DAMAGE.
     36  1.1  thorpej  *
     37  1.1  thorpej  * from	Id: aic7870.c,v 1.37 1996/06/08 06:55:55 gibbs Exp
     38  1.1  thorpej  */
     39  1.1  thorpej 
     40  1.1  thorpej #include <sys/param.h>
     41  1.1  thorpej #include <sys/systm.h>
     42  1.1  thorpej #include <sys/malloc.h>
     43  1.1  thorpej #include <sys/kernel.h>
     44  1.1  thorpej #include <sys/queue.h>
     45  1.1  thorpej #include <sys/device.h>
     46  1.1  thorpej 
     47  1.1  thorpej #include <machine/bus.h>
     48  1.1  thorpej #include <machine/intr.h>
     49  1.1  thorpej 
     50  1.1  thorpej #include <dev/scsipi/scsi_all.h>
     51  1.1  thorpej #include <dev/scsipi/scsipi_all.h>
     52  1.1  thorpej #include <dev/scsipi/scsiconf.h>
     53  1.1  thorpej 
     54  1.1  thorpej #include <dev/ic/aic7xxxreg.h>
     55  1.1  thorpej #include <dev/ic/aic7xxxvar.h>
     56  1.1  thorpej #include <dev/ic/smc93cx6var.h>
     57  1.1  thorpej 
     58  1.1  thorpej /*
     59  1.1  thorpej  * Under normal circumstances, these messages are unnecessary
     60  1.1  thorpej  * and not terribly cosmetic.
     61  1.1  thorpej  */
     62  1.1  thorpej #ifdef DEBUG
     63  1.1  thorpej #define	bootverbose	1
     64  1.1  thorpej #else
     65  1.1  thorpej #define	bootverbose	0
     66  1.1  thorpej #endif
     67  1.1  thorpej 
     68  1.1  thorpej /*
     69  1.1  thorpej  * Define the format of the aic78X0 SEEPROM registers (16 bits).
     70  1.1  thorpej  */
     71  1.1  thorpej 
     72  1.1  thorpej struct seeprom_config {
     73  1.1  thorpej 
     74  1.1  thorpej /*
     75  1.1  thorpej  * SCSI ID Configuration Flags
     76  1.1  thorpej  */
     77  1.1  thorpej #define CFXFER		0x0007		/* synchronous transfer rate */
     78  1.1  thorpej #define CFSYNCH		0x0008		/* enable synchronous transfer */
     79  1.1  thorpej #define CFDISC		0x0010		/* enable disconnection */
     80  1.1  thorpej #define CFWIDEB		0x0020		/* wide bus device */
     81  1.1  thorpej /* UNUSED		0x00C0 */
     82  1.1  thorpej #define CFSTART		0x0100		/* send start unit SCSI command */
     83  1.1  thorpej #define CFINCBIOS	0x0200		/* include in BIOS scan */
     84  1.1  thorpej #define CFRNFOUND	0x0400		/* report even if not found */
     85  1.1  thorpej /* UNUSED		0xf800 */
     86  1.1  thorpej   u_int16_t device_flags[16];	/* words 0-15 */
     87  1.1  thorpej 
     88  1.1  thorpej /*
     89  1.1  thorpej  * BIOS Control Bits
     90  1.1  thorpej  */
     91  1.1  thorpej #define CFSUPREM	0x0001		/* support all removeable drives */
     92  1.1  thorpej #define CFSUPREMB	0x0002		/* support removeable drives for boot only */
     93  1.1  thorpej #define CFBIOSEN	0x0004		/* BIOS enabled */
     94  1.1  thorpej /* UNUSED		0x0008 */
     95  1.1  thorpej #define CFSM2DRV	0x0010		/* support more than two drives */
     96  1.1  thorpej /* UNUSED		0x0060 */
     97  1.1  thorpej #define CFEXTEND	0x0080		/* extended translation enabled */
     98  1.1  thorpej /* UNUSED		0xff00 */
     99  1.1  thorpej   u_int16_t bios_control;		/* word 16 */
    100  1.1  thorpej 
    101  1.1  thorpej /*
    102  1.1  thorpej  * Host Adapter Control Bits
    103  1.1  thorpej  */
    104  1.1  thorpej /* UNUSED		0x0001 */
    105  1.1  thorpej #define CFULTRAEN       0x0002          /* Ultra SCSI speed enable (Ultra cards) */
    106  1.1  thorpej #define CFSTERM		0x0004		/* SCSI low byte termination (non-wide cards) */
    107  1.1  thorpej #define CFWSTERM	0x0008		/* SCSI high byte termination (wide card) */
    108  1.1  thorpej #define CFSPARITY	0x0010		/* SCSI parity */
    109  1.1  thorpej /* UNUSED		0x0020 */
    110  1.1  thorpej #define CFRESETB	0x0040		/* reset SCSI bus at IC initialization */
    111  1.1  thorpej /* UNUSED		0xff80 */
    112  1.1  thorpej   u_int16_t adapter_control;	/* word 17 */
    113  1.1  thorpej 
    114  1.1  thorpej /*
    115  1.1  thorpej  * Bus Release, Host Adapter ID
    116  1.1  thorpej  */
    117  1.1  thorpej #define CFSCSIID	0x000f		/* host adapter SCSI ID */
    118  1.1  thorpej /* UNUSED		0x00f0 */
    119  1.1  thorpej #define CFBRTIME	0xff00		/* bus release time */
    120  1.1  thorpej  u_int16_t brtime_id;		/* word 18 */
    121  1.1  thorpej 
    122  1.1  thorpej /*
    123  1.1  thorpej  * Maximum targets
    124  1.1  thorpej  */
    125  1.1  thorpej #define CFMAXTARG	0x00ff	/* maximum targets */
    126  1.1  thorpej /* UNUSED		0xff00 */
    127  1.1  thorpej   u_int16_t max_targets;		/* word 19 */
    128  1.1  thorpej 
    129  1.1  thorpej   u_int16_t res_1[11];		/* words 20-30 */
    130  1.1  thorpej   u_int16_t checksum;		/* word 31 */
    131  1.1  thorpej };
    132  1.1  thorpej 
    133  1.1  thorpej int ahc_acquire_seeprom __P((struct seeprom_descriptor *sd));
    134  1.1  thorpej void ahc_release_seeprom __P((struct seeprom_descriptor *sd));
    135  1.1  thorpej 
    136  1.1  thorpej /*
    137  1.1  thorpej  * Read the SEEPROM.  Return 0 on failure
    138  1.1  thorpej  */
    139  1.1  thorpej void
    140  1.1  thorpej ahc_load_seeprom(ahc)
    141  1.1  thorpej 	struct	ahc_data *ahc;
    142  1.1  thorpej {
    143  1.1  thorpej 	struct	seeprom_descriptor sd;
    144  1.1  thorpej 	struct	seeprom_config sc;
    145  1.1  thorpej 	u_short *scarray = (u_short *)&sc;
    146  1.1  thorpej 	u_short	checksum = 0;
    147  1.1  thorpej 	u_char	scsi_conf;
    148  1.1  thorpej 	u_char	host_id;
    149  1.1  thorpej 	int	have_seeprom;
    150  1.1  thorpej 
    151  1.1  thorpej 	sd.sd_st = ahc->sc_st;
    152  1.1  thorpej 	sd.sd_sh = ahc->sc_sh;
    153  1.1  thorpej 	sd.sd_offset = SEECTL;
    154  1.1  thorpej 	sd.sd_MS = SEEMS;
    155  1.1  thorpej 	sd.sd_RDY = SEERDY;
    156  1.1  thorpej 	sd.sd_CS = SEECS;
    157  1.1  thorpej 	sd.sd_CK = SEECK;
    158  1.1  thorpej 	sd.sd_DO = SEEDO;
    159  1.1  thorpej 	sd.sd_DI = SEEDI;
    160  1.1  thorpej 
    161  1.1  thorpej 	if(bootverbose)
    162  1.1  thorpej 		printf("%s: Reading SEEPROM...", ahc_name(ahc));
    163  1.1  thorpej 	have_seeprom = ahc_acquire_seeprom(&sd);
    164  1.1  thorpej 	if (have_seeprom) {
    165  1.1  thorpej 		have_seeprom = read_seeprom(&sd,
    166  1.1  thorpej 					    (u_int16_t *)&sc,
    167  1.1  thorpej 					    ahc->flags & AHC_CHNLB,
    168  1.1  thorpej 					    sizeof(sc)/2);
    169  1.1  thorpej 		ahc_release_seeprom(&sd);
    170  1.1  thorpej 		if (have_seeprom) {
    171  1.1  thorpej 			/* Check checksum */
    172  1.1  thorpej 			int i;
    173  1.1  thorpej 
    174  1.1  thorpej 			for (i = 0;i < (sizeof(sc)/2 - 1);i = i + 1)
    175  1.1  thorpej 				checksum = checksum + scarray[i];
    176  1.1  thorpej 			if (checksum != sc.checksum) {
    177  1.1  thorpej 				if(bootverbose)
    178  1.1  thorpej 					printf ("checksum error");
    179  1.1  thorpej 				have_seeprom = 0;
    180  1.1  thorpej 			}
    181  1.1  thorpej 			else if(bootverbose)
    182  1.1  thorpej 				printf("done.\n");
    183  1.1  thorpej 		}
    184  1.1  thorpej 	}
    185  1.1  thorpej 	if (!have_seeprom) {
    186  1.1  thorpej 		if(bootverbose)
    187  1.1  thorpej 			printf("\n%s: No SEEPROM availible\n", ahc_name(ahc));
    188  1.1  thorpej 		ahc->flags |= AHC_USEDEFAULTS;
    189  1.1  thorpej 	}
    190  1.1  thorpej 	else {
    191  1.1  thorpej 		/*
    192  1.1  thorpej 		 * Put the data we've collected down into SRAM
    193  1.1  thorpej 		 * where ahc_init will find it.
    194  1.1  thorpej 		 */
    195  1.1  thorpej 		int i;
    196  1.1  thorpej 		int max_targ = sc.max_targets & CFMAXTARG;
    197  1.1  thorpej 
    198  1.1  thorpej 	        for(i = 0; i < max_targ; i++){
    199  1.1  thorpej 	                u_char target_settings;
    200  1.1  thorpej 			target_settings = (sc.device_flags[i] & CFXFER) << 4;
    201  1.1  thorpej 			if (sc.device_flags[i] & CFSYNCH)
    202  1.1  thorpej 				target_settings |= SOFS;
    203  1.1  thorpej 			if (sc.device_flags[i] & CFWIDEB)
    204  1.1  thorpej 				target_settings |= WIDEXFER;
    205  1.1  thorpej 			if (sc.device_flags[i] & CFDISC)
    206  1.1  thorpej 				ahc->discenable |= (0x01 << i);
    207  1.1  thorpej 			AHC_OUTB(ahc, TARG_SCRATCH+i, target_settings);
    208  1.1  thorpej 		}
    209  1.1  thorpej 		AHC_OUTB(ahc, DISC_DSB, ~(ahc->discenable & 0xff));
    210  1.1  thorpej 		AHC_OUTB(ahc, DISC_DSB + 1, ~((ahc->discenable >> 8) & 0xff));
    211  1.1  thorpej 
    212  1.1  thorpej 		host_id = sc.brtime_id & CFSCSIID;
    213  1.1  thorpej 
    214  1.1  thorpej 		scsi_conf = (host_id & 0x7);
    215  1.1  thorpej 		if(sc.adapter_control & CFSPARITY)
    216  1.1  thorpej 			scsi_conf |= ENSPCHK;
    217  1.1  thorpej 		if(sc.adapter_control & CFRESETB)
    218  1.1  thorpej 			scsi_conf |= RESET_SCSI;
    219  1.1  thorpej 
    220  1.1  thorpej 		if(ahc->type & AHC_ULTRA) {
    221  1.1  thorpej 			/* Should we enable Ultra mode? */
    222  1.1  thorpej 			if(!(sc.adapter_control & CFULTRAEN))
    223  1.1  thorpej 				/* Treat us as a non-ultra card */
    224  1.1  thorpej 				ahc->type &= ~AHC_ULTRA;
    225  1.1  thorpej 		}
    226  1.1  thorpej 		/* Set the host ID */
    227  1.1  thorpej 		AHC_OUTB(ahc, SCSICONF, scsi_conf);
    228  1.1  thorpej 		/* In case we are a wide card */
    229  1.1  thorpej 		AHC_OUTB(ahc, SCSICONF + 1, host_id);
    230  1.1  thorpej 	}
    231  1.1  thorpej }
    232  1.1  thorpej 
    233  1.1  thorpej int
    234  1.1  thorpej ahc_acquire_seeprom(sd)
    235  1.1  thorpej 	struct seeprom_descriptor *sd;
    236  1.1  thorpej {
    237  1.1  thorpej 	int wait;
    238  1.1  thorpej 
    239  1.1  thorpej 	/*
    240  1.1  thorpej 	 * Request access of the memory port.  When access is
    241  1.1  thorpej 	 * granted, SEERDY will go high.  We use a 1 second
    242  1.1  thorpej 	 * timeout which should be near 1 second more than
    243  1.1  thorpej 	 * is needed.  Reason: after the chip reset, there
    244  1.1  thorpej 	 * should be no contention.
    245  1.1  thorpej 	 */
    246  1.1  thorpej 	SEEPROM_OUTB(sd, sd->sd_MS);
    247  1.1  thorpej 	wait = 1000;  /* 1 second timeout in msec */
    248  1.1  thorpej 	while (--wait && ((SEEPROM_INB(sd) & sd->sd_RDY) == 0)) {
    249  1.1  thorpej 		DELAY (1000);  /* delay 1 msec */
    250  1.1  thorpej         }
    251  1.1  thorpej 	if ((SEEPROM_INB(sd) & sd->sd_RDY) == 0) {
    252  1.1  thorpej 		SEEPROM_OUTB(sd, 0);
    253  1.1  thorpej 		return (0);
    254  1.1  thorpej 	}
    255  1.1  thorpej 	return(1);
    256  1.1  thorpej }
    257  1.1  thorpej 
    258  1.1  thorpej void
    259  1.1  thorpej ahc_release_seeprom(sd)
    260  1.1  thorpej 	struct seeprom_descriptor *sd;
    261  1.1  thorpej {
    262  1.1  thorpej 	/* Release access to the memory port and the serial EEPROM. */
    263  1.1  thorpej 	SEEPROM_OUTB(sd, 0);
    264  1.1  thorpej }
    265