Home | History | Annotate | Line # | Download | only in pci
ahc_pci.c revision 1.10
      1  1.10       cgd /*	$NetBSD: ahc_pci.c,v 1.10 1997/03/13 00:38:52 cgd Exp $	*/
      2   1.2   thorpej 
      3   1.1   mycroft /*
      4   1.1   mycroft  * Product specific probe and attach routines for:
      5   1.1   mycroft  *      3940, 2940, aic7880, aic7870, aic7860 and aic7850 SCSI controllers
      6   1.1   mycroft  *
      7   1.1   mycroft  * Copyright (c) 1995, 1996 Justin T. Gibbs.
      8   1.1   mycroft  * All rights reserved.
      9   1.1   mycroft  *
     10   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     11   1.1   mycroft  * modification, are permitted provided that the following conditions
     12   1.1   mycroft  * are met:
     13   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     14   1.1   mycroft  *    notice immediately at the beginning of the file, without modification,
     15   1.1   mycroft  *    this list of conditions, and the following disclaimer.
     16   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     18   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     19   1.1   mycroft  * 3. The name of the author may not be used to endorse or promote products
     20   1.1   mycroft  *    derived from this software without specific prior written permission.
     21   1.1   mycroft  *
     22   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     23   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1   mycroft  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     26   1.1   mycroft  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1   mycroft  * SUCH DAMAGE.
     33   1.3  explorer  *
     34   1.3  explorer  * from	Id: aic7870.c,v 1.37 1996/06/08 06:55:55 gibbs Exp
     35   1.1   mycroft  */
     36   1.1   mycroft 
     37   1.1   mycroft #if defined(__FreeBSD__)
     38   1.1   mycroft #include <pci.h>
     39   1.1   mycroft #endif
     40   1.1   mycroft #if NPCI > 0 || defined(__NetBSD__)
     41   1.1   mycroft #include <sys/param.h>
     42   1.1   mycroft #include <sys/systm.h>
     43   1.1   mycroft #include <sys/malloc.h>
     44   1.1   mycroft #include <sys/kernel.h>
     45   1.1   mycroft #include <sys/queue.h>
     46   1.1   mycroft #if defined(__NetBSD__)
     47   1.1   mycroft #include <sys/device.h>
     48   1.1   mycroft #include <machine/bus.h>
     49   1.1   mycroft #include <machine/intr.h>
     50   1.1   mycroft #endif /* defined(__NetBSD__) */
     51   1.1   mycroft 
     52   1.1   mycroft #include <scsi/scsi_all.h>
     53   1.1   mycroft #include <scsi/scsiconf.h>
     54   1.1   mycroft 
     55   1.1   mycroft #if defined(__FreeBSD__)
     56   1.1   mycroft 
     57   1.1   mycroft #include <pci/pcireg.h>
     58   1.1   mycroft #include <pci/pcivar.h>
     59   1.1   mycroft 
     60   1.1   mycroft #include <machine/clock.h>
     61   1.1   mycroft 
     62   1.1   mycroft #include <i386/scsi/aic7xxx.h>
     63   1.1   mycroft #include <i386/scsi/93cx6.h>
     64   1.1   mycroft 
     65   1.1   mycroft #include <dev/aic7xxx/aic7xxx_reg.h>
     66   1.1   mycroft 
     67   1.1   mycroft #define PCI_BASEADR0	PCI_MAP_REG_START
     68   1.1   mycroft 
     69   1.1   mycroft #elif defined(__NetBSD__)
     70   1.1   mycroft 
     71   1.1   mycroft #include <dev/pci/pcireg.h>
     72   1.1   mycroft #include <dev/pci/pcivar.h>
     73   1.1   mycroft 
     74   1.1   mycroft #include <dev/ic/aic7xxxreg.h>
     75   1.1   mycroft #include <dev/ic/aic7xxxvar.h>
     76   1.1   mycroft #include <dev/ic/smc93cx6var.h>
     77   1.1   mycroft 
     78   1.5   thorpej /*
     79   1.5   thorpej  * Under normal circumstances, these messages are unnecessary
     80   1.5   thorpej  * and not terribly cosmetic.
     81   1.5   thorpej  */
     82   1.5   thorpej #ifdef DEBUG
     83   1.1   mycroft #define bootverbose	1
     84   1.5   thorpej #else
     85   1.5   thorpej #define bootverbose	0
     86   1.5   thorpej #endif
     87   1.5   thorpej 
     88   1.1   mycroft #define PCI_BASEADR0	PCI_MAPREG_START
     89   1.1   mycroft 
     90   1.1   mycroft #endif /* defined(__NetBSD__) */
     91   1.1   mycroft 
     92   1.1   mycroft #define PCI_DEVICE_ID_ADAPTEC_3940U	0x82789004ul
     93   1.1   mycroft #define PCI_DEVICE_ID_ADAPTEC_2944U	0x84789004ul
     94   1.1   mycroft #define PCI_DEVICE_ID_ADAPTEC_2940U	0x81789004ul
     95   1.6     gibbs #define PCI_DEVICE_ID_ADAPTEC_2940AU	0x61789004ul
     96   1.1   mycroft #define PCI_DEVICE_ID_ADAPTEC_3940	0x72789004ul
     97   1.1   mycroft #define PCI_DEVICE_ID_ADAPTEC_2944	0x74789004ul
     98   1.1   mycroft #define PCI_DEVICE_ID_ADAPTEC_2940	0x71789004ul
     99   1.1   mycroft #define PCI_DEVICE_ID_ADAPTEC_AIC7880	0x80789004ul
    100   1.1   mycroft #define PCI_DEVICE_ID_ADAPTEC_AIC7870	0x70789004ul
    101   1.1   mycroft #define PCI_DEVICE_ID_ADAPTEC_AIC7860	0x60789004ul
    102   1.1   mycroft #define PCI_DEVICE_ID_ADAPTEC_AIC7855	0x55789004ul
    103   1.1   mycroft #define PCI_DEVICE_ID_ADAPTEC_AIC7850	0x50789004ul
    104   1.1   mycroft 
    105   1.1   mycroft #define	DEVCONFIG		0x40
    106   1.1   mycroft #define		MPORTMODE	0x00000400ul	/* aic7870 only */
    107   1.1   mycroft #define		RAMPSM		0x00000200ul	/* aic7870 only */
    108   1.1   mycroft #define		VOLSENSE	0x00000100ul
    109   1.1   mycroft #define		SCBRAMSEL	0x00000080ul
    110   1.1   mycroft #define		MRDCEN		0x00000040ul
    111   1.1   mycroft #define		EXTSCBTIME	0x00000020ul	/* aic7870 only */
    112   1.1   mycroft #define		EXTSCBPEN	0x00000010ul	/* aic7870 only */
    113   1.1   mycroft #define		BERREN		0x00000008ul
    114   1.1   mycroft #define		DACEN		0x00000004ul
    115   1.1   mycroft #define		STPWLEVEL	0x00000002ul
    116   1.1   mycroft #define		DIFACTNEGEN	0x00000001ul	/* aic7870 only */
    117   1.1   mycroft 
    118   1.1   mycroft #define	CSIZE_LATTIME		0x0c
    119   1.1   mycroft #define		CACHESIZE	0x0000003ful	/* only 5 bits */
    120   1.1   mycroft #define		LATTIME		0x0000ff00ul
    121   1.1   mycroft 
    122   1.1   mycroft /*
    123   1.1   mycroft  * Define the format of the aic78X0 SEEPROM registers (16 bits).
    124   1.1   mycroft  *
    125   1.1   mycroft  */
    126   1.1   mycroft 
    127   1.1   mycroft struct seeprom_config {
    128   1.1   mycroft 
    129   1.1   mycroft /*
    130   1.1   mycroft  * SCSI ID Configuration Flags
    131   1.1   mycroft  */
    132   1.1   mycroft #define CFXFER		0x0007		/* synchronous transfer rate */
    133   1.1   mycroft #define CFSYNCH		0x0008		/* enable synchronous transfer */
    134   1.1   mycroft #define CFDISC		0x0010		/* enable disconnection */
    135   1.1   mycroft #define CFWIDEB		0x0020		/* wide bus device */
    136   1.1   mycroft /* UNUSED		0x00C0 */
    137   1.1   mycroft #define CFSTART		0x0100		/* send start unit SCSI command */
    138   1.1   mycroft #define CFINCBIOS	0x0200		/* include in BIOS scan */
    139   1.1   mycroft #define CFRNFOUND	0x0400		/* report even if not found */
    140   1.1   mycroft /* UNUSED		0xf800 */
    141   1.1   mycroft   u_int16_t device_flags[16];	/* words 0-15 */
    142   1.1   mycroft 
    143   1.1   mycroft /*
    144   1.1   mycroft  * BIOS Control Bits
    145   1.1   mycroft  */
    146   1.1   mycroft #define CFSUPREM	0x0001		/* support all removeable drives */
    147   1.1   mycroft #define CFSUPREMB	0x0002		/* support removeable drives for boot only */
    148   1.1   mycroft #define CFBIOSEN	0x0004		/* BIOS enabled */
    149   1.1   mycroft /* UNUSED		0x0008 */
    150   1.1   mycroft #define CFSM2DRV	0x0010		/* support more than two drives */
    151   1.1   mycroft /* UNUSED		0x0060 */
    152   1.1   mycroft #define CFEXTEND	0x0080		/* extended translation enabled */
    153   1.1   mycroft /* UNUSED		0xff00 */
    154   1.1   mycroft   u_int16_t bios_control;		/* word 16 */
    155   1.1   mycroft 
    156   1.1   mycroft /*
    157   1.1   mycroft  * Host Adapter Control Bits
    158   1.1   mycroft  */
    159   1.1   mycroft /* UNUSED		0x0001 */
    160   1.1   mycroft #define CFULTRAEN       0x0002          /* Ultra SCSI speed enable (Ultra cards) */
    161   1.1   mycroft #define CFSTERM		0x0004		/* SCSI low byte termination (non-wide cards) */
    162   1.1   mycroft #define CFWSTERM	0x0008		/* SCSI high byte termination (wide card) */
    163   1.1   mycroft #define CFSPARITY	0x0010		/* SCSI parity */
    164   1.1   mycroft /* UNUSED		0x0020 */
    165   1.1   mycroft #define CFRESETB	0x0040		/* reset SCSI bus at IC initialization */
    166   1.1   mycroft /* UNUSED		0xff80 */
    167   1.1   mycroft   u_int16_t adapter_control;	/* word 17 */
    168   1.1   mycroft 
    169   1.1   mycroft /*
    170   1.1   mycroft  * Bus Release, Host Adapter ID
    171   1.1   mycroft  */
    172   1.1   mycroft #define CFSCSIID	0x000f		/* host adapter SCSI ID */
    173   1.1   mycroft /* UNUSED		0x00f0 */
    174   1.1   mycroft #define CFBRTIME	0xff00		/* bus release time */
    175   1.1   mycroft  u_int16_t brtime_id;		/* word 18 */
    176   1.1   mycroft 
    177   1.1   mycroft /*
    178   1.1   mycroft  * Maximum targets
    179   1.1   mycroft  */
    180   1.1   mycroft #define CFMAXTARG	0x00ff	/* maximum targets */
    181   1.1   mycroft /* UNUSED		0xff00 */
    182   1.1   mycroft   u_int16_t max_targets;		/* word 19 */
    183   1.1   mycroft 
    184   1.1   mycroft   u_int16_t res_1[11];		/* words 20-30 */
    185   1.1   mycroft   u_int16_t checksum;		/* word 31 */
    186   1.1   mycroft };
    187   1.1   mycroft 
    188   1.1   mycroft static void load_seeprom __P((struct ahc_data *ahc));
    189   1.3  explorer static int acquire_seeprom __P((struct seeprom_descriptor *sd));
    190   1.3  explorer static void release_seeprom __P((struct seeprom_descriptor *sd));
    191   1.1   mycroft 
    192   1.1   mycroft static u_char aic3940_count;
    193   1.1   mycroft 
    194   1.1   mycroft #if defined(__FreeBSD__)
    195   1.1   mycroft 
    196   1.1   mycroft static char* aic7870_probe __P((pcici_t tag, pcidi_t type));
    197   1.1   mycroft static void aic7870_attach __P((pcici_t config_id, int unit));
    198   1.1   mycroft 
    199   1.1   mycroft static struct  pci_device ahc_pci_driver = {
    200   1.1   mycroft 	"ahc",
    201   1.1   mycroft         aic7870_probe,
    202   1.1   mycroft         aic7870_attach,
    203   1.1   mycroft         &ahc_unit,
    204   1.1   mycroft 	NULL
    205   1.1   mycroft };
    206   1.1   mycroft 
    207   1.1   mycroft DATA_SET (pcidevice_set, ahc_pci_driver);
    208   1.1   mycroft 
    209   1.1   mycroft static  char*
    210   1.1   mycroft aic7870_probe (pcici_t tag, pcidi_t type)
    211   1.1   mycroft {
    212   1.1   mycroft 	switch(type) {
    213   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_3940U:
    214   1.1   mycroft 			return ("Adaptec 3940 Ultra SCSI host adapter");
    215   1.1   mycroft 			break;
    216   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_3940:
    217   1.1   mycroft 			return ("Adaptec 3940 SCSI host adapter");
    218   1.1   mycroft 			break;
    219   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_2944U:
    220   1.1   mycroft 			return ("Adaptec 2944 Ultra SCSI host adapter");
    221   1.1   mycroft 			break;
    222   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_2940U:
    223   1.1   mycroft 			return ("Adaptec 2940 Ultra SCSI host adapter");
    224   1.1   mycroft 			break;
    225   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_2944:
    226   1.1   mycroft 			return ("Adaptec 2944 SCSI host adapter");
    227   1.1   mycroft 			break;
    228   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_2940:
    229   1.1   mycroft 			return ("Adaptec 2940 SCSI host adapter");
    230   1.1   mycroft 			break;
    231   1.6     gibbs 		case PCI_DEVICE_ID_ADAPTEC_2940AU:
    232   1.6     gibbs 			return ("Adaptec 2940A Ultra SCSI host adapter");
    233   1.6     gibbs 			break;
    234   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_AIC7880:
    235   1.1   mycroft 			return ("Adaptec aic7880 Ultra SCSI host adapter");
    236   1.1   mycroft 			break;
    237   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_AIC7870:
    238   1.1   mycroft 			return ("Adaptec aic7870 SCSI host adapter");
    239   1.1   mycroft 			break;
    240   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_AIC7860:
    241   1.1   mycroft 			return ("Adaptec aic7860 SCSI host adapter");
    242   1.1   mycroft 			break;
    243   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_AIC7855:
    244   1.1   mycroft 			return ("Adaptec aic7855 SCSI host adapter");
    245   1.1   mycroft 			break;
    246   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_AIC7850:
    247   1.1   mycroft 			return ("Adaptec aic7850 SCSI host adapter");
    248   1.1   mycroft 			break;
    249   1.1   mycroft 		default:
    250   1.1   mycroft 			break;
    251   1.1   mycroft 	}
    252   1.1   mycroft 	return (0);
    253   1.1   mycroft 
    254   1.1   mycroft }
    255   1.1   mycroft 
    256   1.1   mycroft #elif defined(__NetBSD__)
    257   1.1   mycroft 
    258  1.10       cgd #ifdef	__BROKEN_INDIRECT_CONFIG
    259   1.1   mycroft int ahc_pci_probe __P((struct device *, void *, void *));
    260  1.10       cgd #else
    261  1.10       cgd int ahc_pci_probe __P((struct device *, struct cfdata *, void *));
    262  1.10       cgd #endif
    263   1.1   mycroft void ahc_pci_attach __P((struct device *, struct device *, void *));
    264   1.1   mycroft 
    265   1.1   mycroft struct cfattach ahc_pci_ca = {
    266   1.1   mycroft 	sizeof(struct ahc_data), ahc_pci_probe, ahc_pci_attach
    267   1.1   mycroft };
    268   1.1   mycroft 
    269   1.1   mycroft int
    270   1.1   mycroft ahc_pci_probe(parent, match, aux)
    271   1.1   mycroft         struct device *parent;
    272  1.10       cgd #ifdef	__BROKEN_INDIRECT_CONFIG
    273   1.1   mycroft         void *match, *aux;
    274  1.10       cgd #else
    275  1.10       cgd         struct cfdata *match; void *aux;
    276  1.10       cgd #endif
    277   1.1   mycroft {
    278   1.1   mycroft         struct pci_attach_args *pa = aux;
    279   1.1   mycroft 
    280   1.1   mycroft 	switch (pa->pa_id) {
    281   1.1   mycroft 	case PCI_DEVICE_ID_ADAPTEC_3940U:
    282   1.1   mycroft 	case PCI_DEVICE_ID_ADAPTEC_2944U:
    283   1.1   mycroft 	case PCI_DEVICE_ID_ADAPTEC_2940U:
    284   1.6     gibbs 	case PCI_DEVICE_ID_ADAPTEC_2940AU:
    285   1.1   mycroft 	case PCI_DEVICE_ID_ADAPTEC_3940:
    286   1.1   mycroft 	case PCI_DEVICE_ID_ADAPTEC_2944:
    287   1.1   mycroft 	case PCI_DEVICE_ID_ADAPTEC_2940:
    288   1.1   mycroft 	case PCI_DEVICE_ID_ADAPTEC_AIC7880:
    289   1.1   mycroft 	case PCI_DEVICE_ID_ADAPTEC_AIC7870:
    290   1.1   mycroft 	case PCI_DEVICE_ID_ADAPTEC_AIC7860:
    291   1.1   mycroft 	case PCI_DEVICE_ID_ADAPTEC_AIC7855:
    292   1.1   mycroft 	case PCI_DEVICE_ID_ADAPTEC_AIC7850:
    293   1.1   mycroft 		return 1;
    294   1.1   mycroft 	}
    295   1.1   mycroft 	return 0;
    296   1.1   mycroft }
    297   1.1   mycroft #endif /* defined(__NetBSD__) */
    298   1.1   mycroft 
    299   1.1   mycroft #if defined(__FreeBSD__)
    300   1.1   mycroft static void
    301   1.1   mycroft aic7870_attach(config_id, unit)
    302   1.1   mycroft 	pcici_t config_id;
    303   1.1   mycroft 	int	unit;
    304   1.1   mycroft #elif defined(__NetBSD__)
    305   1.1   mycroft void
    306   1.1   mycroft ahc_pci_attach(parent, self, aux)
    307   1.1   mycroft         struct device *parent, *self;
    308   1.1   mycroft         void *aux;
    309   1.1   mycroft #endif
    310   1.1   mycroft {
    311   1.1   mycroft #if defined(__FreeBSD__)
    312   1.1   mycroft 	u_long io_port;
    313   1.7  christos 	int unit = ahc->sc_dev.dv_unit;
    314   1.1   mycroft #elif defined(__NetBSD__)
    315   1.1   mycroft 	struct pci_attach_args *pa = aux;
    316   1.1   mycroft 	struct ahc_data *ahc = (void *)self;
    317   1.9   thorpej 	bus_addr_t iobase;
    318   1.9   thorpej 	bus_size_t iosize;
    319   1.9   thorpej 	bus_space_handle_t ioh;
    320   1.1   mycroft 	pci_intr_handle_t ih;
    321   1.1   mycroft 	const char *intrstr;
    322   1.1   mycroft #endif
    323   1.1   mycroft 	u_long id;
    324   1.1   mycroft 	unsigned opri = 0;
    325   1.1   mycroft 	ahc_type ahc_t = AHC_NONE;
    326   1.1   mycroft 	ahc_flag ahc_f = AHC_FNONE;
    327   1.1   mycroft #if defined(__FreeBSD__)
    328   1.1   mycroft 	struct ahc_data *ahc;
    329   1.1   mycroft #endif
    330   1.1   mycroft 	u_char ultra_enb = 0;
    331   1.1   mycroft 	u_char our_id = 0;
    332   1.1   mycroft 
    333   1.1   mycroft #if defined(__FreeBSD__)
    334   1.1   mycroft         if(!(io_port = pci_conf_read(config_id, PCI_BASEADR0)))
    335   1.1   mycroft 		return;
    336   1.1   mycroft 	/*
    337   1.1   mycroft 	 * The first bit of PCI_BASEADR0 is always
    338   1.1   mycroft 	 * set hence we mask it off.
    339   1.1   mycroft 	 */
    340   1.1   mycroft 	io_port &= 0xfffffffe;
    341   1.1   mycroft #elif defined(__NetBSD__)
    342   1.1   mycroft 	if (pci_io_find(pa->pa_pc, pa->pa_tag, PCI_BASEADR0, &iobase, &iosize))
    343   1.1   mycroft 		return;
    344   1.9   thorpej 	if (bus_space_map(pa->pa_iot, iobase, iosize, 0, &ioh))
    345   1.1   mycroft 		return;
    346   1.1   mycroft #endif
    347   1.1   mycroft 
    348   1.1   mycroft #if defined(__FreeBSD__)
    349   1.1   mycroft 	switch ((id = pci_conf_read(config_id, PCI_ID_REG))) {
    350   1.1   mycroft #elif defined(__NetBSD__)
    351   1.1   mycroft 	switch (id = pa->pa_id) {
    352   1.1   mycroft #endif
    353   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_3940U:
    354   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_3940:
    355   1.1   mycroft 			if (id == PCI_DEVICE_ID_ADAPTEC_3940U)
    356   1.1   mycroft 				ahc_t = AHC_394U;
    357   1.1   mycroft 			else
    358   1.1   mycroft 				ahc_t = AHC_394;
    359   1.1   mycroft 			aic3940_count++;
    360   1.1   mycroft 			if(!(aic3940_count & 0x01))
    361   1.1   mycroft 				/* Even count implies second channel */
    362   1.1   mycroft 				ahc_f |= AHC_CHNLB;
    363   1.1   mycroft 			break;
    364   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_2944U:
    365   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_2940U:
    366   1.1   mycroft 			ahc_t = AHC_294U;
    367   1.1   mycroft 			break;
    368   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_2944:
    369   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_2940:
    370   1.1   mycroft 			ahc_t = AHC_294;
    371   1.1   mycroft 			break;
    372   1.6     gibbs 		case PCI_DEVICE_ID_ADAPTEC_2940AU:
    373   1.6     gibbs 			ahc_t = AHC_294AU;
    374   1.6     gibbs 			break;
    375   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_AIC7880:
    376   1.1   mycroft 			ahc_t = AHC_AIC7880;
    377   1.1   mycroft 			break;
    378   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_AIC7870:
    379   1.1   mycroft 			ahc_t = AHC_AIC7870;
    380   1.1   mycroft 			break;
    381   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_AIC7860:
    382   1.1   mycroft 			ahc_t = AHC_AIC7860;
    383   1.1   mycroft 			break;
    384   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_AIC7855:
    385   1.1   mycroft 		case PCI_DEVICE_ID_ADAPTEC_AIC7850:
    386   1.1   mycroft 			ahc_t = AHC_AIC7850;
    387   1.1   mycroft 			break;
    388   1.1   mycroft 		default:
    389   1.1   mycroft 			break;
    390   1.1   mycroft 	}
    391   1.1   mycroft 
    392   1.1   mycroft 	/* On all PCI adapters, we allow SCB paging */
    393   1.1   mycroft 	ahc_f |= AHC_PAGESCBS;
    394   1.1   mycroft 
    395   1.1   mycroft 	/* Remeber how the card was setup in case there is no SEEPROM */
    396   1.1   mycroft #if defined(__FreeBSD__)
    397   1.1   mycroft 	our_id = inb(SCSIID + io_port) & OID;
    398   1.1   mycroft 	if(ahc_t & AHC_ULTRA)
    399   1.1   mycroft 		ultra_enb = inb(SXFRCTL0 + io_port) & ULTRAEN;
    400   1.1   mycroft #else
    401   1.9   thorpej 	our_id = bus_space_read_1(pa->pa_iot, ioh, SCSIID) & OID;
    402   1.1   mycroft 	if(ahc_t & AHC_ULTRA)
    403   1.9   thorpej 		ultra_enb = bus_space_read_1(pa->pa_iot, ioh,
    404   1.9   thorpej 		    SXFRCTL0) & ULTRAEN;
    405   1.1   mycroft #endif
    406   1.1   mycroft 
    407   1.1   mycroft #if defined(__FreeBSD__)
    408   1.1   mycroft 	ahc_reset(io_port);
    409   1.1   mycroft #elif defined(__NetBSD__)
    410   1.8  christos 	printf("\n");
    411   1.9   thorpej 	ahc_reset(ahc->sc_dev.dv_xname, pa->pa_iot, ioh);
    412   1.1   mycroft #endif
    413   1.1   mycroft 
    414   1.1   mycroft 	if(ahc_t & AHC_AIC7870){
    415   1.1   mycroft #if defined(__FreeBSD__)
    416   1.1   mycroft 		u_long devconfig = pci_conf_read(config_id, DEVCONFIG);
    417   1.1   mycroft #elif defined(__NetBSD__)
    418   1.1   mycroft 		u_long devconfig =
    419   1.1   mycroft 			pci_conf_read(pa->pa_pc, pa->pa_tag, DEVCONFIG);
    420   1.1   mycroft #endif
    421   1.1   mycroft 
    422   1.1   mycroft 		if(devconfig & (RAMPSM)) {
    423   1.1   mycroft 			/*
    424   1.1   mycroft 			 * External SRAM present.  Have the probe walk
    425   1.1   mycroft 			 * the SCBs to see how much SRAM we have and set
    426   1.1   mycroft 			 * the number of SCBs accordingly.  We have to
    427   1.1   mycroft 			 * turn off SCBRAMSEL to access the external
    428   1.1   mycroft 			 * SCB SRAM.
    429   1.1   mycroft 			 *
    430   1.1   mycroft 			 * It seems that early versions of the aic7870
    431   1.1   mycroft 			 * didn't use these bits, hence the hack for the
    432   1.1   mycroft 			 * 3940 above.  I would guess that recent 3940s
    433   1.1   mycroft 			 * using later aic7870 or aic7880 chips do
    434   1.1   mycroft 			 * actually set RAMPSM.
    435   1.1   mycroft 			 *
    436   1.1   mycroft 			 * The documentation isn't clear, but it sounds
    437   1.1   mycroft 			 * like the value written to devconfig must not
    438   1.1   mycroft 			 * have RAMPSM set.  The second sixteen bits of
    439   1.1   mycroft 			 * the register are R/O anyway, so it shouldn't
    440   1.1   mycroft 			 * affect RAMPSM either way.
    441   1.1   mycroft 			 */
    442   1.1   mycroft 			devconfig &= ~(RAMPSM|SCBRAMSEL);
    443   1.1   mycroft #if defined(__FreeBSD__)
    444   1.1   mycroft 			pci_conf_write(config_id, DEVCONFIG, devconfig);
    445   1.1   mycroft #elif defined(__NetBSD__)
    446   1.1   mycroft 			pci_conf_write(pa->pa_pc, pa->pa_tag,
    447   1.1   mycroft 				       DEVCONFIG, devconfig);
    448   1.1   mycroft #endif
    449   1.1   mycroft 		}
    450   1.1   mycroft 	}
    451   1.1   mycroft 
    452   1.1   mycroft #if defined(__FreeBSD__)
    453   1.1   mycroft 	if(!(ahc = ahc_alloc(unit, io_port, ahc_t, ahc_f)))
    454   1.1   mycroft 		return;  /* XXX PCI code should take return status */
    455   1.1   mycroft 
    456   1.1   mycroft 	if(!(pci_map_int(config_id, ahc_intr, (void *)ahc, &bio_imask))) {
    457   1.1   mycroft 		ahc_free(ahc);
    458   1.1   mycroft 		return;
    459   1.1   mycroft 	}
    460   1.1   mycroft #elif defined(__NetBSD__)
    461   1.9   thorpej 	ahc_construct(ahc, pa->pa_iot, ioh, ahc_t, ahc_f);
    462   1.1   mycroft 
    463   1.1   mycroft 	if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
    464   1.1   mycroft 			 pa->pa_intrline, &ih)) {
    465   1.8  christos 		printf("%s: couldn't map interrupt\n", ahc->sc_dev.dv_xname);
    466   1.1   mycroft 		ahc_free(ahc);
    467   1.1   mycroft 		return;
    468   1.1   mycroft 	}
    469   1.1   mycroft 	intrstr = pci_intr_string(pa->pa_pc, ih);
    470   1.6     gibbs #if defined(__OpenBSD__)
    471   1.6     gibbs 	ahc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, ahc_intr, ahc,
    472   1.6     gibbs 					ahc->sc_dev.dv_xname);
    473   1.6     gibbs #else
    474   1.4       cgd 	ahc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, ahc_intr, ahc);
    475   1.6     gibbs #endif
    476   1.1   mycroft 	if (ahc->sc_ih == NULL) {
    477   1.8  christos 		printf("%s: couldn't establish interrupt",
    478   1.1   mycroft 		       ahc->sc_dev.dv_xname);
    479   1.1   mycroft 		if (intrstr != NULL)
    480   1.8  christos 			printf(" at %s", intrstr);
    481   1.8  christos 		printf("\n");
    482   1.1   mycroft 		ahc_free(ahc);
    483   1.1   mycroft 		return;
    484   1.1   mycroft 	}
    485   1.1   mycroft 	if (intrstr != NULL)
    486   1.8  christos 		printf("%s: interrupting at %s\n", ahc->sc_dev.dv_xname,
    487   1.1   mycroft 		       intrstr);
    488   1.1   mycroft #endif
    489   1.1   mycroft 	/*
    490   1.1   mycroft 	 * Protect ourself from spurrious interrupts during
    491   1.1   mycroft 	 * intialization.
    492   1.1   mycroft 	 */
    493   1.1   mycroft 	opri = splbio();
    494   1.1   mycroft 
    495   1.1   mycroft 	/*
    496   1.1   mycroft 	 * Do aic7870/aic7880/aic7850 specific initialization
    497   1.1   mycroft 	 */
    498   1.1   mycroft 	{
    499   1.1   mycroft 		u_char	sblkctl;
    500   1.1   mycroft 		char	*id_string;
    501   1.1   mycroft 
    502   1.1   mycroft 		switch(ahc->type) {
    503   1.1   mycroft 		   case AHC_394U:
    504   1.1   mycroft 		   case AHC_294U:
    505   1.1   mycroft 		   case AHC_AIC7880:
    506   1.1   mycroft 		   {
    507   1.1   mycroft 			id_string = "aic7880 ";
    508   1.1   mycroft 			load_seeprom(ahc);
    509   1.1   mycroft 			break;
    510   1.1   mycroft 		   }
    511   1.1   mycroft 		   case AHC_394:
    512   1.1   mycroft 		   case AHC_294:
    513   1.1   mycroft 		   case AHC_AIC7870:
    514   1.1   mycroft 		   {
    515   1.1   mycroft 			id_string = "aic7870 ";
    516   1.1   mycroft 			load_seeprom(ahc);
    517   1.1   mycroft 			break;
    518   1.1   mycroft 		   }
    519   1.6     gibbs 		   case AHC_294AU:
    520   1.1   mycroft 		   case AHC_AIC7860:
    521   1.1   mycroft 		   {
    522   1.1   mycroft 			id_string = "aic7860 ";
    523   1.6     gibbs 			load_seeprom(ahc);
    524   1.1   mycroft 			break;
    525   1.1   mycroft 		   }
    526   1.1   mycroft 		   case AHC_AIC7850:
    527   1.1   mycroft 		   {
    528   1.1   mycroft 			id_string = "aic7850 ";
    529   1.1   mycroft 			/*
    530   1.1   mycroft 			 * Use defaults, if the chip wasn't initialized by
    531   1.1   mycroft 			 * a BIOS.
    532   1.1   mycroft 			 */
    533   1.1   mycroft 			ahc->flags |= AHC_USEDEFAULTS;
    534   1.1   mycroft 			break;
    535   1.1   mycroft 		   }
    536   1.1   mycroft 		   default:
    537   1.1   mycroft 		   {
    538   1.8  christos 			printf("ahc: Unknown controller type.  Ignoring.\n");
    539   1.1   mycroft 			ahc_free(ahc);
    540   1.1   mycroft 			splx(opri);
    541   1.1   mycroft 			return;
    542   1.1   mycroft 		   }
    543   1.1   mycroft 		}
    544   1.1   mycroft 
    545   1.1   mycroft 		/*
    546   1.1   mycroft 		 * Take the LED out of diagnostic mode
    547   1.1   mycroft 		 */
    548   1.1   mycroft 		sblkctl = AHC_INB(ahc, SBLKCTL);
    549   1.1   mycroft 		AHC_OUTB(ahc, SBLKCTL, (sblkctl & ~(DIAGLEDEN|DIAGLEDON)));
    550   1.1   mycroft 
    551   1.1   mycroft 		/*
    552   1.1   mycroft 		 * I don't know where this is set in the SEEPROM or by the
    553   1.1   mycroft 		 * BIOS, so we default to 100%.
    554   1.1   mycroft 		 */
    555   1.1   mycroft 		AHC_OUTB(ahc, DSPCISTATUS, DFTHRSH_100);
    556   1.1   mycroft 
    557   1.1   mycroft 		if(ahc->flags & AHC_USEDEFAULTS) {
    558   1.1   mycroft 			/*
    559   1.1   mycroft 			 * PCI Adapter default setup
    560   1.1   mycroft 			 * Should only be used if the adapter does not have
    561   1.1   mycroft 			 * an SEEPROM.
    562   1.1   mycroft 			 */
    563   1.1   mycroft 			/* See if someone else set us up already */
    564   1.1   mycroft 			u_long i;
    565   1.1   mycroft 		        for(i = TARG_SCRATCH; i < 0x60; i++) {
    566   1.3  explorer                         	if(AHC_INB(ahc, i) != 0x00)
    567   1.1   mycroft 					break;
    568   1.1   mycroft 			}
    569   1.3  explorer 			if(i == TARG_SCRATCH) {
    570   1.3  explorer 				/*
    571   1.3  explorer 				 * Try looking for all ones.  You can get
    572   1.3  explorer 				 * either.
    573   1.3  explorer 				 */
    574   1.3  explorer 		        	for (i = TARG_SCRATCH; i < 0x60; i++) {
    575   1.3  explorer                         		if(AHC_INB(ahc, i) != 0xff)
    576   1.3  explorer 						break;
    577   1.3  explorer 				}
    578   1.3  explorer 			}
    579   1.3  explorer 			if((i != 0x60) && (our_id != 0)) {
    580   1.8  christos 				printf("%s: Using left over BIOS settings\n",
    581   1.1   mycroft 					ahc_name(ahc));
    582   1.1   mycroft 				ahc->flags &= ~AHC_USEDEFAULTS;
    583   1.1   mycroft 			}
    584   1.1   mycroft 			else
    585   1.1   mycroft 				our_id = 0x07;
    586   1.1   mycroft 			AHC_OUTB(ahc, SCSICONF,
    587   1.1   mycroft 				 (our_id & 0x07)|ENSPCHK|RESET_SCSI);
    588   1.1   mycroft 			/* In case we are a wide card */
    589   1.1   mycroft 			AHC_OUTB(ahc, SCSICONF + 1, our_id);
    590   1.1   mycroft 
    591   1.1   mycroft 			if(!ultra_enb || (ahc->flags & AHC_USEDEFAULTS)) {
    592   1.1   mycroft 				/*
    593   1.1   mycroft 				 * If there wasn't a BIOS or the board
    594   1.1   mycroft 				 * wasn't in this mode to begin with,
    595   1.1   mycroft 				 * turn off ultra.
    596   1.1   mycroft 				 */
    597   1.1   mycroft 				ahc->type &= ~AHC_ULTRA;
    598   1.1   mycroft 			}
    599   1.1   mycroft 		}
    600   1.1   mycroft 
    601   1.8  christos 		printf("%s: %s", ahc_name(ahc), id_string);
    602   1.1   mycroft 	}
    603   1.1   mycroft 
    604   1.1   mycroft 	if(ahc_init(ahc)){
    605   1.1   mycroft 		ahc_free(ahc);
    606   1.1   mycroft 		splx(opri);
    607   1.1   mycroft 		return; /* XXX PCI code should take return status */
    608   1.1   mycroft 	}
    609   1.1   mycroft 	splx(opri);
    610   1.1   mycroft 
    611   1.1   mycroft 	ahc_attach(ahc);
    612   1.1   mycroft }
    613   1.1   mycroft 
    614   1.1   mycroft /*
    615   1.1   mycroft  * Read the SEEPROM.  Return 0 on failure
    616   1.1   mycroft  */
    617   1.1   mycroft void
    618   1.1   mycroft load_seeprom(ahc)
    619   1.1   mycroft 	struct	ahc_data *ahc;
    620   1.1   mycroft {
    621   1.1   mycroft 	struct	seeprom_descriptor sd;
    622   1.1   mycroft 	struct	seeprom_config sc;
    623   1.1   mycroft 	u_short *scarray = (u_short *)&sc;
    624   1.1   mycroft 	u_short	checksum = 0;
    625   1.1   mycroft 	u_char	scsi_conf;
    626   1.1   mycroft 	u_char	host_id;
    627   1.1   mycroft 	int	have_seeprom;
    628   1.1   mycroft 
    629   1.1   mycroft #if defined(__FreeBSD__)
    630   1.1   mycroft 	sd.sd_iobase = ahc->baseport + SEECTL;
    631   1.1   mycroft #elif defined(__NetBSD__)
    632   1.9   thorpej 	sd.sd_iot = ahc->sc_iot;
    633   1.1   mycroft 	sd.sd_ioh = ahc->sc_ioh;
    634   1.1   mycroft 	sd.sd_offset = SEECTL;
    635   1.1   mycroft #endif
    636   1.1   mycroft 	sd.sd_MS = SEEMS;
    637   1.1   mycroft 	sd.sd_RDY = SEERDY;
    638   1.1   mycroft 	sd.sd_CS = SEECS;
    639   1.1   mycroft 	sd.sd_CK = SEECK;
    640   1.1   mycroft 	sd.sd_DO = SEEDO;
    641   1.1   mycroft 	sd.sd_DI = SEEDI;
    642   1.1   mycroft 
    643   1.1   mycroft 	if(bootverbose)
    644   1.8  christos 		printf("%s: Reading SEEPROM...", ahc_name(ahc));
    645   1.1   mycroft 	have_seeprom = acquire_seeprom(&sd);
    646   1.1   mycroft 	if (have_seeprom) {
    647   1.1   mycroft 		have_seeprom = read_seeprom(&sd,
    648   1.1   mycroft 					    (u_int16_t *)&sc,
    649   1.1   mycroft 					    ahc->flags & AHC_CHNLB,
    650   1.1   mycroft 					    sizeof(sc)/2);
    651   1.1   mycroft 		release_seeprom(&sd);
    652   1.1   mycroft 		if (have_seeprom) {
    653   1.1   mycroft 			/* Check checksum */
    654   1.1   mycroft 			int i;
    655   1.1   mycroft 
    656   1.1   mycroft 			for (i = 0;i < (sizeof(sc)/2 - 1);i = i + 1)
    657   1.1   mycroft 				checksum = checksum + scarray[i];
    658   1.1   mycroft 			if (checksum != sc.checksum) {
    659   1.1   mycroft 				if(bootverbose)
    660   1.8  christos 					printf ("checksum error");
    661   1.1   mycroft 				have_seeprom = 0;
    662   1.1   mycroft 			}
    663   1.1   mycroft 			else if(bootverbose)
    664   1.8  christos 				printf("done.\n");
    665   1.1   mycroft 		}
    666   1.1   mycroft 	}
    667   1.1   mycroft 	if (!have_seeprom) {
    668   1.1   mycroft 		if(bootverbose)
    669   1.8  christos 			printf("\n%s: No SEEPROM availible\n", ahc_name(ahc));
    670   1.1   mycroft 		ahc->flags |= AHC_USEDEFAULTS;
    671   1.1   mycroft 	}
    672   1.1   mycroft 	else {
    673   1.1   mycroft 		/*
    674   1.1   mycroft 		 * Put the data we've collected down into SRAM
    675   1.1   mycroft 		 * where ahc_init will find it.
    676   1.1   mycroft 		 */
    677   1.1   mycroft 		int i;
    678   1.1   mycroft 		int max_targ = sc.max_targets & CFMAXTARG;
    679   1.1   mycroft 
    680   1.3  explorer 	        for(i = 0; i < max_targ; i++){
    681   1.1   mycroft 	                u_char target_settings;
    682   1.1   mycroft 			target_settings = (sc.device_flags[i] & CFXFER) << 4;
    683   1.1   mycroft 			if (sc.device_flags[i] & CFSYNCH)
    684   1.1   mycroft 				target_settings |= SOFS;
    685   1.1   mycroft 			if (sc.device_flags[i] & CFWIDEB)
    686   1.1   mycroft 				target_settings |= WIDEXFER;
    687   1.1   mycroft 			if (sc.device_flags[i] & CFDISC)
    688   1.1   mycroft 				ahc->discenable |= (0x01 << i);
    689   1.1   mycroft 			AHC_OUTB(ahc, TARG_SCRATCH+i, target_settings);
    690   1.1   mycroft 		}
    691   1.1   mycroft 		AHC_OUTB(ahc, DISC_DSB, ~(ahc->discenable & 0xff));
    692   1.1   mycroft 		AHC_OUTB(ahc, DISC_DSB + 1, ~((ahc->discenable >> 8) & 0xff));
    693   1.1   mycroft 
    694   1.1   mycroft 		host_id = sc.brtime_id & CFSCSIID;
    695   1.1   mycroft 
    696   1.1   mycroft 		scsi_conf = (host_id & 0x7);
    697   1.1   mycroft 		if(sc.adapter_control & CFSPARITY)
    698   1.1   mycroft 			scsi_conf |= ENSPCHK;
    699   1.1   mycroft 		if(sc.adapter_control & CFRESETB)
    700   1.1   mycroft 			scsi_conf |= RESET_SCSI;
    701   1.1   mycroft 
    702   1.1   mycroft 		if(ahc->type & AHC_ULTRA) {
    703   1.1   mycroft 			/* Should we enable Ultra mode? */
    704   1.1   mycroft 			if(!(sc.adapter_control & CFULTRAEN))
    705   1.1   mycroft 				/* Treat us as a non-ultra card */
    706   1.1   mycroft 				ahc->type &= ~AHC_ULTRA;
    707   1.1   mycroft 		}
    708   1.1   mycroft 		/* Set the host ID */
    709   1.1   mycroft 		AHC_OUTB(ahc, SCSICONF, scsi_conf);
    710   1.1   mycroft 		/* In case we are a wide card */
    711   1.1   mycroft 		AHC_OUTB(ahc, SCSICONF + 1, host_id);
    712   1.1   mycroft 	}
    713   1.3  explorer }
    714   1.3  explorer 
    715   1.3  explorer static int
    716   1.3  explorer acquire_seeprom(sd)
    717   1.3  explorer 	struct seeprom_descriptor *sd;
    718   1.3  explorer {
    719   1.3  explorer 	int wait;
    720   1.3  explorer 
    721   1.3  explorer 	/*
    722   1.3  explorer 	 * Request access of the memory port.  When access is
    723   1.3  explorer 	 * granted, SEERDY will go high.  We use a 1 second
    724   1.3  explorer 	 * timeout which should be near 1 second more than
    725   1.3  explorer 	 * is needed.  Reason: after the chip reset, there
    726   1.3  explorer 	 * should be no contention.
    727   1.3  explorer 	 */
    728   1.3  explorer 	SEEPROM_OUTB(sd, sd->sd_MS);
    729   1.3  explorer 	wait = 1000;  /* 1 second timeout in msec */
    730   1.3  explorer 	while (--wait && ((SEEPROM_INB(sd) & sd->sd_RDY) == 0)) {
    731   1.3  explorer 		DELAY (1000);  /* delay 1 msec */
    732   1.3  explorer         }
    733   1.3  explorer 	if ((SEEPROM_INB(sd) & sd->sd_RDY) == 0) {
    734   1.3  explorer 		SEEPROM_OUTB(sd, 0);
    735   1.3  explorer 		return (0);
    736   1.3  explorer 	}
    737   1.3  explorer 	return(1);
    738   1.3  explorer }
    739   1.3  explorer 
    740   1.3  explorer static void
    741   1.3  explorer release_seeprom(sd)
    742   1.3  explorer 	struct seeprom_descriptor *sd;
    743   1.3  explorer {
    744   1.3  explorer 	/* Release access to the memory port and the serial EEPROM. */
    745   1.3  explorer 	SEEPROM_OUTB(sd, 0);
    746   1.1   mycroft }
    747   1.1   mycroft 
    748   1.1   mycroft #endif /* NPCI > 0 */
    749