Home | History | Annotate | Line # | Download | only in ic
adwlib.c revision 1.5.2.1
      1  1.5.2.1  bouyer /* $NetBSD: adwlib.c,v 1.5.2.1 2000/11/20 11:40:08 bouyer Exp $        */
      2      1.1   dante 
      3      1.1   dante /*
      4      1.1   dante  * Low level routines for the Advanced Systems Inc. SCSI controllers chips
      5      1.1   dante  *
      6  1.5.2.1  bouyer  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
      7      1.1   dante  * All rights reserved.
      8      1.1   dante  *
      9      1.1   dante  * Author: Baldassare Dante Profeta <dante (at) mclink.it>
     10      1.1   dante  *
     11      1.1   dante  * Redistribution and use in source and binary forms, with or without
     12      1.1   dante  * modification, are permitted provided that the following conditions
     13      1.1   dante  * are met:
     14      1.1   dante  * 1. Redistributions of source code must retain the above copyright
     15      1.1   dante  *    notice, this list of conditions and the following disclaimer.
     16      1.1   dante  * 2. Redistributions in binary form must reproduce the above copyright
     17      1.1   dante  *    notice, this list of conditions and the following disclaimer in the
     18      1.1   dante  *    documentation and/or other materials provided with the distribution.
     19      1.1   dante  * 3. All advertising materials mentioning features or use of this software
     20      1.1   dante  *    must display the following acknowledgement:
     21      1.1   dante  *        This product includes software developed by the NetBSD
     22      1.1   dante  *        Foundation, Inc. and its contributors.
     23      1.1   dante  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24      1.1   dante  *    contributors may be used to endorse or promote products derived
     25      1.1   dante  *    from this software without specific prior written permission.
     26      1.1   dante  *
     27      1.1   dante  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28      1.1   dante  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29      1.1   dante  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30      1.1   dante  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31      1.1   dante  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32      1.1   dante  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.1   dante  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.1   dante  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35      1.1   dante  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.1   dante  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37      1.1   dante  * POSSIBILITY OF SUCH DAMAGE.
     38      1.1   dante  */
     39      1.1   dante /*
     40      1.1   dante  * Ported from:
     41      1.1   dante  */
     42      1.1   dante /*
     43      1.1   dante  * advansys.c - Linux Host Driver for AdvanSys SCSI Adapters
     44  1.5.2.1  bouyer  *
     45  1.5.2.1  bouyer  * Copyright (c) 1995-2000 Advanced System Products, Inc.
     46      1.1   dante  * All Rights Reserved.
     47      1.1   dante  *
     48      1.1   dante  * Redistribution and use in source and binary forms, with or without
     49      1.1   dante  * modification, are permitted provided that redistributions of source
     50      1.1   dante  * code retain the above copyright notice and this comment without
     51      1.1   dante  * modification.
     52      1.1   dante  */
     53      1.1   dante 
     54      1.1   dante #include <sys/types.h>
     55      1.1   dante #include <sys/param.h>
     56      1.1   dante #include <sys/systm.h>
     57      1.1   dante #include <sys/malloc.h>
     58      1.1   dante #include <sys/kernel.h>
     59      1.1   dante #include <sys/queue.h>
     60      1.1   dante #include <sys/device.h>
     61      1.1   dante 
     62      1.1   dante #include <machine/bus.h>
     63      1.1   dante #include <machine/intr.h>
     64      1.1   dante 
     65      1.1   dante #include <dev/scsipi/scsi_all.h>
     66      1.1   dante #include <dev/scsipi/scsipi_all.h>
     67      1.1   dante #include <dev/scsipi/scsiconf.h>
     68      1.1   dante 
     69  1.5.2.1  bouyer #include <dev/pci/pcidevs.h>
     70  1.5.2.1  bouyer 
     71  1.5.2.1  bouyer #include <uvm/uvm_extern.h>
     72      1.1   dante 
     73      1.1   dante #include <dev/ic/adwlib.h>
     74      1.1   dante #include <dev/ic/adwmcode.h>
     75  1.5.2.1  bouyer #include <dev/ic/adw.h>
     76      1.1   dante 
     77      1.1   dante 
     78      1.1   dante /* Static Functions */
     79      1.1   dante 
     80  1.5.2.1  bouyer int AdwRamSelfTest __P((bus_space_tag_t, bus_space_handle_t, u_int8_t));
     81  1.5.2.1  bouyer int AdwLoadMCode __P((bus_space_tag_t, bus_space_handle_t, u_int16_t *,
     82  1.5.2.1  bouyer 								u_int8_t));
     83  1.5.2.1  bouyer int AdwASC3550Cabling __P((bus_space_tag_t, bus_space_handle_t, ADW_DVC_CFG *));
     84  1.5.2.1  bouyer int AdwASC38C0800Cabling __P((bus_space_tag_t, bus_space_handle_t,
     85  1.5.2.1  bouyer 								ADW_DVC_CFG *));
     86  1.5.2.1  bouyer int AdwASC38C1600Cabling __P((bus_space_tag_t, bus_space_handle_t,
     87  1.5.2.1  bouyer 								ADW_DVC_CFG *));
     88  1.5.2.1  bouyer 
     89  1.5.2.1  bouyer static u_int16_t AdwGetEEPROMConfig __P((bus_space_tag_t, bus_space_handle_t,
     90  1.5.2.1  bouyer      							ADW_EEPROM *));
     91  1.5.2.1  bouyer static void AdwSetEEPROMConfig __P((bus_space_tag_t, bus_space_handle_t,
     92  1.5.2.1  bouyer 					                 ADW_EEPROM *));
     93  1.5.2.1  bouyer static u_int16_t AdwReadEEPWord __P((bus_space_tag_t, bus_space_handle_t, int));
     94  1.5.2.1  bouyer static void AdwWaitEEPCmd __P((bus_space_tag_t, bus_space_handle_t));
     95      1.1   dante 
     96  1.5.2.1  bouyer static void AdwInquiryHandling __P((ADW_SOFTC *, ADW_SCSI_REQ_Q *));
     97  1.5.2.1  bouyer 
     98  1.5.2.1  bouyer static void AdwSleepMilliSecond __P((u_int32_t));
     99  1.5.2.1  bouyer static void AdwDelayMicroSecond __P((u_int32_t));
    100      1.1   dante 
    101      1.1   dante 
    102      1.1   dante /*
    103      1.1   dante  * EEPROM Configuration.
    104      1.1   dante  *
    105      1.1   dante  * All drivers should use this structure to set the default EEPROM
    106      1.1   dante  * configuration. The BIOS now uses this structure when it is built.
    107  1.5.2.1  bouyer  * Additional structure information can be found in adwlib.h where
    108      1.1   dante  * the structure is defined.
    109      1.1   dante  */
    110  1.5.2.1  bouyer const static ADW_EEPROM adw_3550_Default_EEPROM = {
    111  1.5.2.1  bouyer 	ADW_EEPROM_BIOS_ENABLE,	/* 00 cfg_lsw */
    112  1.5.2.1  bouyer 	0x0000,			/* 01 cfg_msw */
    113  1.5.2.1  bouyer 	0xFFFF,			/* 02 disc_enable */
    114  1.5.2.1  bouyer 	0xFFFF,			/* 03 wdtr_able */
    115  1.5.2.1  bouyer 	{ 0xFFFF },		/* 04 sdtr_able */
    116  1.5.2.1  bouyer 	0xFFFF,			/* 05 start_motor */
    117  1.5.2.1  bouyer 	0xFFFF,			/* 06 tagqng_able */
    118  1.5.2.1  bouyer 	0xFFFF,			/* 07 bios_scan */
    119  1.5.2.1  bouyer 	0,			/* 08 scam_tolerant */
    120  1.5.2.1  bouyer 	7,			/* 09 adapter_scsi_id */
    121  1.5.2.1  bouyer 	0,			/*    bios_boot_delay */
    122  1.5.2.1  bouyer 	3,			/* 10 scsi_reset_delay */
    123  1.5.2.1  bouyer 	0,			/*    bios_id_lun */
    124  1.5.2.1  bouyer 	0,			/* 11 termination */
    125  1.5.2.1  bouyer 	0,			/*    reserved1 */
    126  1.5.2.1  bouyer 	0xFFE7,			/* 12 bios_ctrl */
    127  1.5.2.1  bouyer 	{ 0xFFFF },		/* 13 ultra_able */
    128  1.5.2.1  bouyer 	{ 0 },			/* 14 reserved2 */
    129  1.5.2.1  bouyer 	ADW_DEF_MAX_HOST_QNG,	/* 15 max_host_qng */
    130  1.5.2.1  bouyer 	ADW_DEF_MAX_DVC_QNG,	/*    max_dvc_qng */
    131  1.5.2.1  bouyer 	0,			/* 16 dvc_cntl */
    132  1.5.2.1  bouyer 	{ 0 },			/* 17 bug_fix */
    133  1.5.2.1  bouyer 	{ 0,0,0 },		/* 18-20 serial_number[3] */
    134  1.5.2.1  bouyer 	0,			/* 21 check_sum */
    135  1.5.2.1  bouyer 	{			/* 22-29 oem_name[16] */
    136  1.5.2.1  bouyer 	  0,0,0,0,0,0,0,0,
    137  1.5.2.1  bouyer 	  0,0,0,0,0,0,0,0
    138  1.5.2.1  bouyer 	},
    139  1.5.2.1  bouyer 	0,			/* 30 dvc_err_code */
    140  1.5.2.1  bouyer 	0,			/* 31 adv_err_code */
    141  1.5.2.1  bouyer 	0,			/* 32 adv_err_addr */
    142  1.5.2.1  bouyer 	0,			/* 33 saved_dvc_err_code */
    143  1.5.2.1  bouyer 	0,			/* 34 saved_adv_err_code */
    144  1.5.2.1  bouyer 	0			/* 35 saved_adv_err_addr */
    145  1.5.2.1  bouyer };
    146  1.5.2.1  bouyer 
    147  1.5.2.1  bouyer const static ADW_EEPROM adw_38C0800_Default_EEPROM = {
    148  1.5.2.1  bouyer 	ADW_EEPROM_BIOS_ENABLE,	/* 00 cfg_lsw */
    149  1.5.2.1  bouyer 	0x0000,			/* 01 cfg_msw */
    150  1.5.2.1  bouyer 	0xFFFF,			/* 02 disc_enable */
    151  1.5.2.1  bouyer 	0xFFFF,			/* 03 wdtr_able */
    152  1.5.2.1  bouyer 	{ 0x4444 },		/* 04 sdtr_speed1 */
    153  1.5.2.1  bouyer 	0xFFFF,			/* 05 start_motor */
    154  1.5.2.1  bouyer 	0xFFFF,			/* 06 tagqng_able */
    155  1.5.2.1  bouyer 	0xFFFF,			/* 07 bios_scan */
    156  1.5.2.1  bouyer 	0,			/* 08 scam_tolerant */
    157  1.5.2.1  bouyer 	7,			/* 09 adapter_scsi_id */
    158  1.5.2.1  bouyer 	0,			/*    bios_boot_delay */
    159  1.5.2.1  bouyer 	3,			/* 10 scsi_reset_delay */
    160  1.5.2.1  bouyer 	0,			/*    bios_id_lun */
    161  1.5.2.1  bouyer 	0,			/* 11 termination_se */
    162  1.5.2.1  bouyer 	0,			/*    termination_lvd */
    163  1.5.2.1  bouyer 	0xFFE7,			/* 12 bios_ctrl */
    164  1.5.2.1  bouyer 	{ 0x4444 },		/* 13 sdtr_speed2 */
    165  1.5.2.1  bouyer 	{ 0x4444 },		/* 14 sdtr_speed3 */
    166  1.5.2.1  bouyer 	ADW_DEF_MAX_HOST_QNG,	/* 15 max_host_qng */
    167  1.5.2.1  bouyer 	ADW_DEF_MAX_DVC_QNG,	/*    max_dvc_qng */
    168  1.5.2.1  bouyer 	0,			/* 16 dvc_cntl */
    169  1.5.2.1  bouyer 	{ 0x4444 },		/* 17 sdtr_speed4 */
    170  1.5.2.1  bouyer 	{ 0,0,0 },		/* 18-20 serial_number[3] */
    171  1.5.2.1  bouyer 	0,			/* 21 check_sum */
    172  1.5.2.1  bouyer 	{			/* 22-29 oem_name[16] */
    173  1.5.2.1  bouyer 	  0,0,0,0,0,0,0,0,
    174  1.5.2.1  bouyer 	  0,0,0,0,0,0,0,0
    175  1.5.2.1  bouyer 	},
    176  1.5.2.1  bouyer 	0,			/* 30 dvc_err_code */
    177  1.5.2.1  bouyer 	0,			/* 31 adv_err_code */
    178  1.5.2.1  bouyer 	0,			/* 32 adv_err_addr */
    179  1.5.2.1  bouyer 	0,			/* 33 saved_dvc_err_code */
    180  1.5.2.1  bouyer 	0,			/* 34 saved_adv_err_code */
    181  1.5.2.1  bouyer 	0,			/* 35 saved_adv_err_addr */
    182  1.5.2.1  bouyer 	{			/* 36-55 reserved1[16] */
    183  1.5.2.1  bouyer 	  0,0,0,0,0,0,0,0,0,0,
    184  1.5.2.1  bouyer 	  0,0,0,0,0,0,0,0,0,0
    185  1.5.2.1  bouyer 	},
    186  1.5.2.1  bouyer 	0,			/* 56 cisptr_lsw */
    187  1.5.2.1  bouyer 	0,			/* 57 cisprt_msw */
    188  1.5.2.1  bouyer 	PCI_VENDOR_ADVSYS,	/* 58 subsysvid */
    189  1.5.2.1  bouyer 	PCI_PRODUCT_ADVSYS_U2W,	/* 59 subsysid */
    190  1.5.2.1  bouyer 	{ 0,0,0,0 }		/* 60-63 reserved2[4] */
    191  1.5.2.1  bouyer };
    192  1.5.2.1  bouyer 
    193  1.5.2.1  bouyer const static ADW_EEPROM adw_38C1600_Default_EEPROM = {
    194  1.5.2.1  bouyer 	ADW_EEPROM_BIOS_ENABLE,	/* 00 cfg_lsw */
    195  1.5.2.1  bouyer 	0x0000,			/* 01 cfg_msw */
    196  1.5.2.1  bouyer 	0xFFFF,			/* 02 disc_enable */
    197  1.5.2.1  bouyer 	0xFFFF,			/* 03 wdtr_able */
    198  1.5.2.1  bouyer 	{ 0x5555 },		/* 04 sdtr_speed1 */
    199  1.5.2.1  bouyer 	0xFFFF,			/* 05 start_motor */
    200  1.5.2.1  bouyer 	0xFFFF,			/* 06 tagqng_able */
    201  1.5.2.1  bouyer 	0xFFFF,			/* 07 bios_scan */
    202  1.5.2.1  bouyer 	0,			/* 08 scam_tolerant */
    203  1.5.2.1  bouyer 	7,			/* 09 adapter_scsi_id */
    204  1.5.2.1  bouyer 	0,			/*    bios_boot_delay */
    205  1.5.2.1  bouyer 	3,			/* 10 scsi_reset_delay */
    206  1.5.2.1  bouyer 	0,			/*    bios_id_lun */
    207  1.5.2.1  bouyer 	0,			/* 11 termination_se */
    208  1.5.2.1  bouyer 	0,			/*    termination_lvd */
    209  1.5.2.1  bouyer 	0xFFE7,			/* 12 bios_ctrl */
    210  1.5.2.1  bouyer 	{ 0x5555 },		/* 13 sdtr_speed2 */
    211  1.5.2.1  bouyer 	{ 0x5555 },		/* 14 sdtr_speed3 */
    212  1.5.2.1  bouyer 	ADW_DEF_MAX_HOST_QNG,	/* 15 max_host_qng */
    213  1.5.2.1  bouyer 	ADW_DEF_MAX_DVC_QNG,	/*    max_dvc_qng */
    214  1.5.2.1  bouyer 	0,			/* 16 dvc_cntl */
    215  1.5.2.1  bouyer 	{ 0x5555 },		/* 17 sdtr_speed4 */
    216  1.5.2.1  bouyer 	{ 0,0,0 },		/* 18-20 serial_number[3] */
    217  1.5.2.1  bouyer 	0,			/* 21 check_sum */
    218  1.5.2.1  bouyer 	{			/* 22-29 oem_name[16] */
    219  1.5.2.1  bouyer 	  0,0,0,0,0,0,0,0,
    220  1.5.2.1  bouyer 	  0,0,0,0,0,0,0,0
    221  1.5.2.1  bouyer 	},
    222  1.5.2.1  bouyer 	0,			/* 30 dvc_err_code */
    223  1.5.2.1  bouyer 	0,			/* 31 adv_err_code */
    224  1.5.2.1  bouyer 	0,			/* 32 adv_err_addr */
    225  1.5.2.1  bouyer 	0,			/* 33 saved_dvc_err_code */
    226  1.5.2.1  bouyer 	0,			/* 34 saved_adv_err_code */
    227  1.5.2.1  bouyer 	0,			/* 35 saved_adv_err_addr */
    228  1.5.2.1  bouyer 	{			/* 36-55 reserved1[16] */
    229  1.5.2.1  bouyer 	  0,0,0,0,0,0,0,0,0,0,
    230  1.5.2.1  bouyer 	  0,0,0,0,0,0,0,0,0,0
    231  1.5.2.1  bouyer 	},
    232  1.5.2.1  bouyer 	0,			/* 56 cisptr_lsw */
    233  1.5.2.1  bouyer 	0,			/* 57 cisprt_msw */
    234  1.5.2.1  bouyer 	PCI_VENDOR_ADVSYS,	/* 58 subsysvid */
    235  1.5.2.1  bouyer 	PCI_PRODUCT_ADVSYS_U3W, /* 59 subsysid */
    236  1.5.2.1  bouyer 	{ 0,0,0,0 }		/* 60-63 reserved2[4] */
    237      1.1   dante };
    238      1.1   dante 
    239  1.5.2.1  bouyer 
    240      1.1   dante /*
    241  1.5.2.1  bouyer  * Read the board's EEPROM configuration. Set fields in ADW_SOFTC and
    242  1.5.2.1  bouyer  * ADW_DVC_CFG based on the EEPROM settings. The chip is stopped while
    243  1.5.2.1  bouyer  * all of this is done.
    244      1.1   dante  *
    245      1.1   dante  * For a non-fatal error return a warning code. If there are no warnings
    246      1.1   dante  * then 0 is returned.
    247  1.5.2.1  bouyer  *
    248  1.5.2.1  bouyer  * Note: Chip is stopped on entry.
    249      1.1   dante  */
    250      1.1   dante int
    251  1.5.2.1  bouyer AdwInitFromEEPROM(sc)
    252      1.2   dante ADW_SOFTC      *sc;
    253      1.1   dante {
    254      1.2   dante 	bus_space_tag_t iot = sc->sc_iot;
    255      1.2   dante 	bus_space_handle_t ioh = sc->sc_ioh;
    256  1.5.2.1  bouyer 	ADW_EEPROM		eep_config;
    257  1.5.2.1  bouyer 	u_int16_t		warn_code;
    258  1.5.2.1  bouyer 	u_int16_t		sdtr_speed = 0;
    259  1.5.2.1  bouyer 	u_int8_t		tid, termination;
    260  1.5.2.1  bouyer 	int			i, j;
    261      1.1   dante 
    262      1.1   dante 
    263      1.1   dante 	warn_code = 0;
    264      1.1   dante 
    265      1.1   dante 	/*
    266  1.5.2.1  bouyer 	 * Read the board's EEPROM configuration.
    267      1.1   dante 	 *
    268  1.5.2.1  bouyer 	 * Set default values if a bad checksum is found.
    269  1.5.2.1  bouyer 	 *
    270  1.5.2.1  bouyer 	 * XXX - Don't handle big-endian access to EEPROM yet.
    271      1.1   dante 	 */
    272  1.5.2.1  bouyer 	if (AdwGetEEPROMConfig(iot, ioh, &eep_config) != eep_config.check_sum) {
    273  1.5.2.1  bouyer 		warn_code |= ADW_WARN_EEPROM_CHKSUM;
    274  1.5.2.1  bouyer 
    275  1.5.2.1  bouyer 		/*
    276  1.5.2.1  bouyer 		 * Set EEPROM default values.
    277  1.5.2.1  bouyer 		 */
    278  1.5.2.1  bouyer 		switch(sc->chip_type) {
    279  1.5.2.1  bouyer 		case ADW_CHIP_ASC3550:
    280  1.5.2.1  bouyer 			eep_config = adw_3550_Default_EEPROM;
    281  1.5.2.1  bouyer 			break;
    282  1.5.2.1  bouyer 		case ADW_CHIP_ASC38C0800:
    283  1.5.2.1  bouyer 			eep_config = adw_38C0800_Default_EEPROM;
    284  1.5.2.1  bouyer 			break;
    285  1.5.2.1  bouyer 		case ADW_CHIP_ASC38C1600:
    286  1.5.2.1  bouyer 			eep_config = adw_38C1600_Default_EEPROM;
    287  1.5.2.1  bouyer 
    288  1.5.2.1  bouyer // XXX	  TODO!!!	if (ASC_PCI_ID2FUNC(sc->cfg.pci_slot_info) != 0) {
    289  1.5.2.1  bouyer 			if (sc->cfg.pci_slot_info != 0) {
    290  1.5.2.1  bouyer 				u_int8_t lsw_msb;
    291      1.1   dante 
    292  1.5.2.1  bouyer 				lsw_msb = eep_config.cfg_lsw >> 8;
    293  1.5.2.1  bouyer 				/*
    294  1.5.2.1  bouyer 				 * Set Function 1 EEPROM Word 0 MSB
    295  1.5.2.1  bouyer 				 *
    296  1.5.2.1  bouyer 				 * Clear the BIOS_ENABLE (bit 14) and
    297  1.5.2.1  bouyer 				 * INTAB (bit 11) EEPROM bits.
    298  1.5.2.1  bouyer 				 *
    299  1.5.2.1  bouyer 				 * Disable Bit 14 (BIOS_ENABLE) to fix
    300  1.5.2.1  bouyer 				 * SPARC Ultra 60 and old Mac system booting
    301  1.5.2.1  bouyer 				 * problem. The Expansion ROM must
    302  1.5.2.1  bouyer 				 * be disabled in Function 1 for these systems.
    303  1.5.2.1  bouyer 				 */
    304  1.5.2.1  bouyer 				lsw_msb &= ~(((ADW_EEPROM_BIOS_ENABLE |
    305  1.5.2.1  bouyer 						ADW_EEPROM_INTAB) >> 8) & 0xFF);
    306  1.5.2.1  bouyer 				/*
    307  1.5.2.1  bouyer 				 * Set the INTAB (bit 11) if the GPIO 0 input
    308  1.5.2.1  bouyer 				 * indicates the Function 1 interrupt line is
    309  1.5.2.1  bouyer 				 * wired to INTA.
    310  1.5.2.1  bouyer 				 *
    311  1.5.2.1  bouyer 				 * Set/Clear Bit 11 (INTAB) from
    312  1.5.2.1  bouyer 				 * the GPIO bit 0 input:
    313  1.5.2.1  bouyer 				 *   1 - Function 1 intr line wired to INT A.
    314  1.5.2.1  bouyer 				 *   0 - Function 1 intr line wired to INT B.
    315  1.5.2.1  bouyer 				 *
    316  1.5.2.1  bouyer 				 * Note: Adapter boards always have Function 0
    317  1.5.2.1  bouyer 				 * wired to INTA.
    318  1.5.2.1  bouyer 				 * Put all 5 GPIO bits in input mode and then
    319  1.5.2.1  bouyer 				 * read their input values.
    320  1.5.2.1  bouyer 				 */
    321  1.5.2.1  bouyer 				ADW_WRITE_BYTE_REGISTER(iot, ioh,
    322  1.5.2.1  bouyer 							IOPB_GPIO_CNTL, 0);
    323  1.5.2.1  bouyer 				if (ADW_READ_BYTE_REGISTER(iot, ioh,
    324  1.5.2.1  bouyer 						IOPB_GPIO_DATA) & 0x01) {
    325  1.5.2.1  bouyer 					/*
    326  1.5.2.1  bouyer 					 * Function 1 interrupt wired to INTA;
    327  1.5.2.1  bouyer 					 * Set EEPROM bit.
    328  1.5.2.1  bouyer 					 */
    329  1.5.2.1  bouyer 					lsw_msb |= (ADW_EEPROM_INTAB >> 8)
    330  1.5.2.1  bouyer 							 & 0xFF;
    331  1.5.2.1  bouyer 				 }
    332  1.5.2.1  bouyer 				 eep_config.cfg_lsw &= 0x00FF;
    333  1.5.2.1  bouyer 				 eep_config.cfg_lsw |= lsw_msb << 8;
    334  1.5.2.1  bouyer 			}
    335  1.5.2.1  bouyer 			break;
    336  1.5.2.1  bouyer 		}
    337  1.5.2.1  bouyer 
    338  1.5.2.1  bouyer 		/*
    339  1.5.2.1  bouyer 		 * Assume the 6 byte board serial number that was read
    340  1.5.2.1  bouyer 		 * from EEPROM is correct even if the EEPROM checksum
    341  1.5.2.1  bouyer 		 * failed.
    342  1.5.2.1  bouyer 		 */
    343  1.5.2.1  bouyer 		for (i=2, j=1; i>=0; i--, j++) {
    344  1.5.2.1  bouyer 		eep_config.serial_number[i] =
    345  1.5.2.1  bouyer 			AdwReadEEPWord(iot, ioh, ASC_EEP_DVC_CFG_END - j);
    346  1.5.2.1  bouyer 		}
    347  1.5.2.1  bouyer 
    348  1.5.2.1  bouyer 		AdwSetEEPROMConfig(iot, ioh, &eep_config);
    349  1.5.2.1  bouyer 	}
    350      1.1   dante 	/*
    351  1.5.2.1  bouyer 	 * Set sc and sc->cfg variables from the EEPROM configuration
    352  1.5.2.1  bouyer 	 * that was read.
    353      1.1   dante 	 *
    354  1.5.2.1  bouyer 	 * This is the mapping of EEPROM fields to Adw Library fields.
    355      1.1   dante 	 */
    356  1.5.2.1  bouyer 	sc->wdtr_able = eep_config.wdtr_able;
    357  1.5.2.1  bouyer 	if (sc->chip_type == ADW_CHIP_ASC3550) {
    358  1.5.2.1  bouyer 		sc->sdtr_able = eep_config.sdtr1.sdtr_able;
    359  1.5.2.1  bouyer 		sc->ultra_able = eep_config.sdtr2.ultra_able;
    360  1.5.2.1  bouyer 	} else {
    361  1.5.2.1  bouyer 		sc->sdtr_speed1 = eep_config.sdtr1.sdtr_speed1;
    362  1.5.2.1  bouyer 		sc->sdtr_speed2 = eep_config.sdtr2.sdtr_speed2;
    363  1.5.2.1  bouyer 		sc->sdtr_speed3 = eep_config.sdtr3.sdtr_speed3;
    364  1.5.2.1  bouyer 		sc->sdtr_speed4 = eep_config.sdtr4.sdtr_speed4;
    365  1.5.2.1  bouyer 	}
    366  1.5.2.1  bouyer 	sc->ppr_able = 0;
    367  1.5.2.1  bouyer 	sc->tagqng_able = eep_config.tagqng_able;
    368  1.5.2.1  bouyer 	sc->cfg.disc_enable = eep_config.disc_enable;
    369  1.5.2.1  bouyer 	sc->max_host_qng = eep_config.max_host_qng;
    370  1.5.2.1  bouyer 	sc->max_dvc_qng = eep_config.max_dvc_qng;
    371  1.5.2.1  bouyer 	sc->chip_scsi_id = (eep_config.adapter_scsi_id & ADW_MAX_TID);
    372  1.5.2.1  bouyer 	sc->start_motor = eep_config.start_motor;
    373  1.5.2.1  bouyer 	sc->scsi_reset_wait = eep_config.scsi_reset_delay;
    374  1.5.2.1  bouyer 	sc->bios_ctrl = eep_config.bios_ctrl;
    375  1.5.2.1  bouyer 	sc->no_scam = eep_config.scam_tolerant;
    376  1.5.2.1  bouyer 	sc->cfg.serial1 = eep_config.serial_number[0];
    377  1.5.2.1  bouyer 	sc->cfg.serial2 = eep_config.serial_number[1];
    378  1.5.2.1  bouyer 	sc->cfg.serial3 = eep_config.serial_number[2];
    379  1.5.2.1  bouyer 
    380  1.5.2.1  bouyer 	if (sc->chip_type == ADW_CHIP_ASC38C0800 ||
    381  1.5.2.1  bouyer 	    sc->chip_type == ADW_CHIP_ASC38C1600) {
    382  1.5.2.1  bouyer 		sc->sdtr_able = 0;
    383  1.5.2.1  bouyer 		for (tid = 0; tid <= ADW_MAX_TID; tid++) {
    384  1.5.2.1  bouyer 			if (tid == 0) {
    385  1.5.2.1  bouyer 				sdtr_speed = sc->sdtr_speed1;
    386  1.5.2.1  bouyer 			} else if (tid == 4) {
    387  1.5.2.1  bouyer 				sdtr_speed = sc->sdtr_speed2;
    388  1.5.2.1  bouyer 			} else if (tid == 8) {
    389  1.5.2.1  bouyer 				sdtr_speed = sc->sdtr_speed3;
    390  1.5.2.1  bouyer 			} else if (tid == 12) {
    391  1.5.2.1  bouyer 				sdtr_speed = sc->sdtr_speed4;
    392  1.5.2.1  bouyer 			}
    393  1.5.2.1  bouyer 			if (sdtr_speed & ADW_MAX_TID) {
    394  1.5.2.1  bouyer 				sc->sdtr_able |= (1 << tid);
    395  1.5.2.1  bouyer 			}
    396  1.5.2.1  bouyer 			sdtr_speed >>= 4;
    397  1.5.2.1  bouyer 		}
    398      1.1   dante 	}
    399      1.1   dante 
    400      1.1   dante 	/*
    401  1.5.2.1  bouyer 	 * Set the host maximum queuing (max. 253, min. 16) and the per device
    402  1.5.2.1  bouyer 	 * maximum queuing (max. 63, min. 4).
    403      1.1   dante 	 */
    404  1.5.2.1  bouyer 	if (eep_config.max_host_qng > ADW_DEF_MAX_HOST_QNG) {
    405  1.5.2.1  bouyer 		eep_config.max_host_qng = ADW_DEF_MAX_HOST_QNG;
    406  1.5.2.1  bouyer 	} else if (eep_config.max_host_qng < ADW_DEF_MIN_HOST_QNG)
    407  1.5.2.1  bouyer 	{
    408  1.5.2.1  bouyer 		/* If the value is zero, assume it is uninitialized. */
    409  1.5.2.1  bouyer 		if (eep_config.max_host_qng == 0) {
    410  1.5.2.1  bouyer 			eep_config.max_host_qng = ADW_DEF_MAX_HOST_QNG;
    411  1.5.2.1  bouyer 		} else {
    412  1.5.2.1  bouyer 			eep_config.max_host_qng = ADW_DEF_MIN_HOST_QNG;
    413  1.5.2.1  bouyer 		}
    414  1.5.2.1  bouyer 	}
    415  1.5.2.1  bouyer 
    416  1.5.2.1  bouyer 	if (eep_config.max_dvc_qng > ADW_DEF_MAX_DVC_QNG) {
    417  1.5.2.1  bouyer 		eep_config.max_dvc_qng = ADW_DEF_MAX_DVC_QNG;
    418  1.5.2.1  bouyer 	} else if (eep_config.max_dvc_qng < ADW_DEF_MIN_DVC_QNG) {
    419  1.5.2.1  bouyer 		/* If the value is zero, assume it is uninitialized. */
    420  1.5.2.1  bouyer 		if (eep_config.max_dvc_qng == 0) {
    421  1.5.2.1  bouyer 			eep_config.max_dvc_qng = ADW_DEF_MAX_DVC_QNG;
    422  1.5.2.1  bouyer 		} else {
    423  1.5.2.1  bouyer 			eep_config.max_dvc_qng = ADW_DEF_MIN_DVC_QNG;
    424  1.5.2.1  bouyer 		}
    425      1.1   dante 	}
    426      1.1   dante 
    427      1.1   dante 	/*
    428  1.5.2.1  bouyer 	 * If 'max_dvc_qng' is greater than 'max_host_qng', then
    429  1.5.2.1  bouyer 	 * set 'max_dvc_qng' to 'max_host_qng'.
    430      1.1   dante 	 */
    431  1.5.2.1  bouyer 	if (eep_config.max_dvc_qng > eep_config.max_host_qng) {
    432  1.5.2.1  bouyer 		eep_config.max_dvc_qng = eep_config.max_host_qng;
    433      1.1   dante 	}
    434      1.1   dante 
    435  1.5.2.1  bouyer 	/*
    436  1.5.2.1  bouyer 	 * Set ADV_DVC_VAR 'max_host_qng' and ADV_DVC_VAR 'max_dvc_qng'
    437  1.5.2.1  bouyer 	 * values based on possibly adjusted EEPROM values.
    438  1.5.2.1  bouyer 	 */
    439  1.5.2.1  bouyer 	sc->max_host_qng = eep_config.max_host_qng;
    440  1.5.2.1  bouyer 	sc->max_dvc_qng = eep_config.max_dvc_qng;
    441  1.5.2.1  bouyer 
    442      1.1   dante 
    443      1.1   dante 	/*
    444  1.5.2.1  bouyer 	 * If the EEPROM 'termination' field is set to automatic (0), then set
    445  1.5.2.1  bouyer 	 * the ADV_DVC_CFG 'termination' field to automatic also.
    446  1.5.2.1  bouyer 	 *
    447  1.5.2.1  bouyer 	 * If the termination is specified with a non-zero 'termination'
    448  1.5.2.1  bouyer 	 * value check that a legal value is set and set the ADV_DVC_CFG
    449  1.5.2.1  bouyer 	 * 'termination' field appropriately.
    450  1.5.2.1  bouyer 	 */
    451  1.5.2.1  bouyer 
    452  1.5.2.1  bouyer 	switch(sc->chip_type) {
    453  1.5.2.1  bouyer 	case ADW_CHIP_ASC3550:
    454  1.5.2.1  bouyer 		sc->cfg.termination = 0;	/* auto termination */
    455  1.5.2.1  bouyer 		switch(eep_config.termination_se) {
    456  1.5.2.1  bouyer 		case 3:
    457  1.5.2.1  bouyer 			/* Enable manual control with low on / high on. */
    458  1.5.2.1  bouyer 			sc->cfg.termination |= ADW_TERM_CTL_L;
    459  1.5.2.1  bouyer 		case 2:
    460  1.5.2.1  bouyer 			/* Enable manual control with low off / high on. */
    461  1.5.2.1  bouyer 			sc->cfg.termination |= ADW_TERM_CTL_H;
    462  1.5.2.1  bouyer 		case 1:
    463  1.5.2.1  bouyer 			/* Enable manual control with low off / high off. */
    464  1.5.2.1  bouyer 			sc->cfg.termination |= ADW_TERM_CTL_SEL;
    465  1.5.2.1  bouyer 		case 0:
    466  1.5.2.1  bouyer 			break;
    467  1.5.2.1  bouyer 		default:
    468  1.5.2.1  bouyer 			warn_code |= ADW_WARN_EEPROM_TERMINATION;
    469  1.5.2.1  bouyer 		}
    470  1.5.2.1  bouyer 		break;
    471  1.5.2.1  bouyer 
    472  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C0800:
    473  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C1600:
    474  1.5.2.1  bouyer 		switch(eep_config.termination_se) {
    475  1.5.2.1  bouyer 		case 0:
    476  1.5.2.1  bouyer 			/* auto termination for SE */
    477  1.5.2.1  bouyer 			termination = 0;
    478  1.5.2.1  bouyer 			break;
    479  1.5.2.1  bouyer 		case 1:
    480  1.5.2.1  bouyer 			/* Enable manual control with low off / high off. */
    481  1.5.2.1  bouyer 			termination = 0;
    482  1.5.2.1  bouyer 			break;
    483  1.5.2.1  bouyer 		case 2:
    484  1.5.2.1  bouyer 			/* Enable manual control with low off / high on. */
    485  1.5.2.1  bouyer 			termination = ADW_TERM_SE_HI;
    486  1.5.2.1  bouyer 			break;
    487  1.5.2.1  bouyer 		case 3:
    488  1.5.2.1  bouyer 			/* Enable manual control with low on / high on. */
    489  1.5.2.1  bouyer 			termination = ADW_TERM_SE;
    490  1.5.2.1  bouyer 			break;
    491  1.5.2.1  bouyer 		default:
    492  1.5.2.1  bouyer 			/*
    493  1.5.2.1  bouyer 			 * The EEPROM 'termination_se' field contains a
    494  1.5.2.1  bouyer 			 * bad value. Use automatic termination instead.
    495  1.5.2.1  bouyer 			 */
    496  1.5.2.1  bouyer 			termination = 0;
    497  1.5.2.1  bouyer 			warn_code |= ADW_WARN_EEPROM_TERMINATION;
    498  1.5.2.1  bouyer 		}
    499  1.5.2.1  bouyer 
    500  1.5.2.1  bouyer 		switch(eep_config.termination_lvd) {
    501  1.5.2.1  bouyer 		case 0:
    502  1.5.2.1  bouyer 			/* auto termination for LVD */
    503  1.5.2.1  bouyer 			sc->cfg.termination = termination;
    504  1.5.2.1  bouyer 			break;
    505  1.5.2.1  bouyer 		case 1:
    506  1.5.2.1  bouyer 			/* Enable manual control with low off / high off. */
    507  1.5.2.1  bouyer 			sc->cfg.termination = termination;
    508  1.5.2.1  bouyer 			break;
    509  1.5.2.1  bouyer 		case 2:
    510  1.5.2.1  bouyer 			/* Enable manual control with low off / high on. */
    511  1.5.2.1  bouyer 			sc->cfg.termination = termination | ADW_TERM_LVD_HI;
    512  1.5.2.1  bouyer 			break;
    513  1.5.2.1  bouyer 		case 3:
    514  1.5.2.1  bouyer 			/* Enable manual control with low on / high on. */
    515  1.5.2.1  bouyer 			sc->cfg.termination = termination | ADW_TERM_LVD;
    516  1.5.2.1  bouyer 			break;
    517  1.5.2.1  bouyer 		default:
    518  1.5.2.1  bouyer 			/*
    519  1.5.2.1  bouyer 			 * The EEPROM 'termination_lvd' field contains a
    520  1.5.2.1  bouyer 			 * bad value. Use automatic termination instead.
    521  1.5.2.1  bouyer 			 */
    522  1.5.2.1  bouyer 			sc->cfg.termination = termination;
    523  1.5.2.1  bouyer 			warn_code |= ADW_WARN_EEPROM_TERMINATION;
    524  1.5.2.1  bouyer 		}
    525  1.5.2.1  bouyer 		break;
    526  1.5.2.1  bouyer 	}
    527  1.5.2.1  bouyer 
    528  1.5.2.1  bouyer 	return warn_code;
    529  1.5.2.1  bouyer }
    530  1.5.2.1  bouyer 
    531  1.5.2.1  bouyer 
    532  1.5.2.1  bouyer /*
    533  1.5.2.1  bouyer  * Initialize the ASC-3550/ASC-38C0800/ASC-38C1600.
    534  1.5.2.1  bouyer  *
    535  1.5.2.1  bouyer  * On failure return the error code.
    536  1.5.2.1  bouyer  */
    537  1.5.2.1  bouyer int
    538  1.5.2.1  bouyer AdwInitDriver(sc)
    539  1.5.2.1  bouyer ADW_SOFTC      *sc;
    540  1.5.2.1  bouyer {
    541  1.5.2.1  bouyer 	bus_space_tag_t iot = sc->sc_iot;
    542  1.5.2.1  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
    543  1.5.2.1  bouyer 	u_int16_t	error_code;
    544  1.5.2.1  bouyer 	int		word;
    545  1.5.2.1  bouyer 	int		i;
    546  1.5.2.1  bouyer 	u_int16_t	bios_mem[ADW_MC_BIOSLEN/2];	/* BIOS RISC Memory
    547  1.5.2.1  bouyer 								0x40-0x8F. */
    548  1.5.2.1  bouyer 	u_int16_t	wdtr_able = 0, sdtr_able, ppr_able, tagqng_able;
    549  1.5.2.1  bouyer 	u_int8_t	max_cmd[ADW_MAX_TID + 1];
    550  1.5.2.1  bouyer 	u_int8_t	tid;
    551  1.5.2.1  bouyer 
    552  1.5.2.1  bouyer 
    553  1.5.2.1  bouyer 	error_code = 0;
    554  1.5.2.1  bouyer 
    555  1.5.2.1  bouyer 	/*
    556  1.5.2.1  bouyer 	 * Save the RISC memory BIOS region before writing the microcode.
    557  1.5.2.1  bouyer 	 * The BIOS may already be loaded and using its RISC LRAM region
    558  1.5.2.1  bouyer 	 * so its region must be saved and restored.
    559  1.5.2.1  bouyer 	 *
    560  1.5.2.1  bouyer 	 * Note: This code makes the assumption, which is currently true,
    561  1.5.2.1  bouyer 	 * that a chip reset does not clear RISC LRAM.
    562      1.1   dante 	 */
    563  1.5.2.1  bouyer 	for (i = 0; i < ADW_MC_BIOSLEN/2; i++) {
    564  1.5.2.1  bouyer 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_BIOSMEM+(2*i), bios_mem[i]);
    565      1.1   dante 	}
    566      1.1   dante 
    567      1.1   dante 	/*
    568  1.5.2.1  bouyer 	 * Save current per TID negotiated values.
    569      1.1   dante 	 */
    570  1.5.2.1  bouyer 	switch (sc->chip_type) {
    571  1.5.2.1  bouyer 	case ADW_CHIP_ASC3550:
    572  1.5.2.1  bouyer 		if (bios_mem[(ADW_MC_BIOS_SIGNATURE-ADW_MC_BIOSMEM)/2]==0x55AA){
    573  1.5.2.1  bouyer 
    574  1.5.2.1  bouyer 			u_int16_t  bios_version, major, minor;
    575  1.5.2.1  bouyer 
    576  1.5.2.1  bouyer 			bios_version = bios_mem[(ADW_MC_BIOS_VERSION -
    577  1.5.2.1  bouyer 					ADW_MC_BIOSMEM) / 2];
    578  1.5.2.1  bouyer 			major = (bios_version  >> 12) & 0xF;
    579  1.5.2.1  bouyer 			minor = (bios_version  >> 8) & 0xF;
    580  1.5.2.1  bouyer 			if (major < 3 || (major == 3 && minor == 1)) {
    581  1.5.2.1  bouyer 			    /*
    582  1.5.2.1  bouyer 			     * BIOS 3.1 and earlier location of
    583  1.5.2.1  bouyer 			     * 'wdtr_able' variable.
    584  1.5.2.1  bouyer 			     */
    585  1.5.2.1  bouyer 			    ADW_READ_WORD_LRAM(iot, ioh, 0x120, wdtr_able);
    586  1.5.2.1  bouyer 			} else {
    587  1.5.2.1  bouyer 			    ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE,
    588  1.5.2.1  bouyer 					    wdtr_able);
    589  1.5.2.1  bouyer 			}
    590  1.5.2.1  bouyer 		}
    591  1.5.2.1  bouyer 		break;
    592  1.5.2.1  bouyer 
    593  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C1600:
    594  1.5.2.1  bouyer 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_PPR_ABLE, ppr_able);
    595  1.5.2.1  bouyer 		/* FALLTHROUGH */
    596  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C0800:
    597  1.5.2.1  bouyer 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE, wdtr_able);
    598  1.5.2.1  bouyer 		break;
    599  1.5.2.1  bouyer 	}
    600  1.5.2.1  bouyer 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, sdtr_able);
    601  1.5.2.1  bouyer 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE, tagqng_able);
    602  1.5.2.1  bouyer 	for (tid = 0; tid <= ADW_MAX_TID; tid++) {
    603  1.5.2.1  bouyer 		ADW_READ_BYTE_LRAM(iot, ioh, ADW_MC_NUMBER_OF_MAX_CMD + tid,
    604  1.5.2.1  bouyer 			max_cmd[tid]);
    605      1.1   dante 	}
    606      1.1   dante 
    607      1.1   dante 	/*
    608  1.5.2.1  bouyer 	 * Perform a RAM Built-In Self Test
    609  1.5.2.1  bouyer 	 */
    610  1.5.2.1  bouyer 	if((error_code = AdwRamSelfTest(iot, ioh, sc->chip_type))) {
    611  1.5.2.1  bouyer 		return error_code;
    612  1.5.2.1  bouyer 	}
    613  1.5.2.1  bouyer 
    614  1.5.2.1  bouyer 	/*
    615  1.5.2.1  bouyer 	 * Load the Microcode
    616      1.1   dante 	 */
    617  1.5.2.1  bouyer 	;
    618  1.5.2.1  bouyer 	if((error_code = AdwLoadMCode(iot, ioh, bios_mem, sc->chip_type))) {
    619  1.5.2.1  bouyer 		return error_code;
    620  1.5.2.1  bouyer 	}
    621      1.1   dante 
    622      1.1   dante 	/*
    623  1.5.2.1  bouyer 	 * Read microcode version and date.
    624      1.1   dante 	 */
    625  1.5.2.1  bouyer 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_VERSION_DATE, sc->cfg.mcode_date);
    626  1.5.2.1  bouyer 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_VERSION_NUM, sc->cfg.mcode_version);
    627      1.1   dante 
    628      1.1   dante 	/*
    629      1.1   dante 	 * If the PCI Configuration Command Register "Parity Error Response
    630      1.1   dante 	 * Control" Bit was clear (0), then set the microcode variable
    631      1.1   dante 	 * 'control_flag' CONTROL_FLAG_IGNORE_PERR flag to tell the microcode
    632      1.1   dante 	 * to ignore DMA parity errors.
    633      1.1   dante 	 */
    634      1.1   dante 	if (sc->cfg.control_flag & CONTROL_FLAG_IGNORE_PERR) {
    635  1.5.2.1  bouyer 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_CONTROL_FLAG, word);
    636  1.5.2.1  bouyer 		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_CONTROL_FLAG,
    637  1.5.2.1  bouyer 					word | CONTROL_FLAG_IGNORE_PERR);
    638  1.5.2.1  bouyer 	}
    639  1.5.2.1  bouyer 
    640  1.5.2.1  bouyer 	switch (sc->chip_type) {
    641  1.5.2.1  bouyer 	case ADW_CHIP_ASC3550:
    642  1.5.2.1  bouyer 		/*
    643  1.5.2.1  bouyer 		 * For ASC-3550, setting the START_CTL_EMFU [3:2] bits sets a
    644  1.5.2.1  bouyer 		 * FIFO threshold of 128 bytes.
    645  1.5.2.1  bouyer 		 * This register is only accessible to the host.
    646  1.5.2.1  bouyer 		 */
    647  1.5.2.1  bouyer 		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_DMA_CFG0,
    648  1.5.2.1  bouyer 				START_CTL_EMFU | READ_CMD_MRM);
    649  1.5.2.1  bouyer 		break;
    650  1.5.2.1  bouyer 
    651  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C0800:
    652  1.5.2.1  bouyer 		/*
    653  1.5.2.1  bouyer 		 * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
    654  1.5.2.1  bouyer 		 * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
    655  1.5.2.1  bouyer 		 * cable detection and then we are able to read C_DET[3:0].
    656  1.5.2.1  bouyer 		 *
    657  1.5.2.1  bouyer 		 * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
    658  1.5.2.1  bouyer 		 * Microcode Default Value' section below.
    659  1.5.2.1  bouyer 		 */
    660  1.5.2.1  bouyer 		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1,
    661  1.5.2.1  bouyer 				ADW_READ_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1)
    662  1.5.2.1  bouyer 				| ADW_DIS_TERM_DRV);
    663  1.5.2.1  bouyer 
    664  1.5.2.1  bouyer 		/*
    665  1.5.2.1  bouyer 		 * For ASC-38C0800, set FIFO_THRESH_80B [6:4] bits and
    666  1.5.2.1  bouyer 		 * START_CTL_TH [3:2] bits for the default FIFO threshold.
    667  1.5.2.1  bouyer 		 *
    668  1.5.2.1  bouyer 		 * Note: ASC-38C0800 FIFO threshold has been changed to
    669  1.5.2.1  bouyer 		 * 256 bytes.
    670  1.5.2.1  bouyer 		 *
    671  1.5.2.1  bouyer 		 * For DMA Errata #4 set the BC_THRESH_ENB bit.
    672  1.5.2.1  bouyer 		 */
    673  1.5.2.1  bouyer 		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_DMA_CFG0,
    674  1.5.2.1  bouyer 						BC_THRESH_ENB | FIFO_THRESH_80B
    675  1.5.2.1  bouyer 						| START_CTL_TH | READ_CMD_MRM);
    676  1.5.2.1  bouyer 		break;
    677  1.5.2.1  bouyer 
    678  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C1600:
    679  1.5.2.1  bouyer 		/*
    680  1.5.2.1  bouyer 		 * Write 1 to bit 14 'DIS_TERM_DRV' in the SCSI_CFG1 register.
    681  1.5.2.1  bouyer 		 * When DIS_TERM_DRV set to 1, C_DET[3:0] will reflect current
    682  1.5.2.1  bouyer 		 * cable detection and then we are able to read C_DET[3:0].
    683  1.5.2.1  bouyer 		 *
    684  1.5.2.1  bouyer 		 * Note: We will reset DIS_TERM_DRV to 0 in the 'Set SCSI_CFG1
    685  1.5.2.1  bouyer 		 * Microcode Default Value' section below.
    686  1.5.2.1  bouyer 		 */
    687  1.5.2.1  bouyer 		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1,
    688  1.5.2.1  bouyer 				ADW_READ_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1)
    689  1.5.2.1  bouyer 				| ADW_DIS_TERM_DRV);
    690  1.5.2.1  bouyer 
    691  1.5.2.1  bouyer 		/*
    692  1.5.2.1  bouyer 		 * If the BIOS control flag AIPP (Asynchronous Information
    693  1.5.2.1  bouyer 		 * Phase Protection) disable bit is not set, then set the
    694  1.5.2.1  bouyer 		 * firmware 'control_flag' CONTROL_FLAG_ENABLE_AIPP bit to
    695  1.5.2.1  bouyer 		 * enable AIPP checking and encoding.
    696  1.5.2.1  bouyer 		 */
    697  1.5.2.1  bouyer 		if ((sc->bios_ctrl & BIOS_CTRL_AIPP_DIS) == 0) {
    698  1.5.2.1  bouyer 			ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_CONTROL_FLAG, word);
    699  1.5.2.1  bouyer 			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_CONTROL_FLAG,
    700  1.5.2.1  bouyer 					word | CONTROL_FLAG_ENABLE_AIPP);
    701  1.5.2.1  bouyer 		}
    702  1.5.2.1  bouyer 
    703  1.5.2.1  bouyer 		/*
    704  1.5.2.1  bouyer 		 * For ASC-38C1600 use DMA_CFG0 default values:
    705  1.5.2.1  bouyer 		 * FIFO_THRESH_80B [6:4], and START_CTL_TH [3:2].
    706  1.5.2.1  bouyer 		 */
    707  1.5.2.1  bouyer 		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_DMA_CFG0,
    708  1.5.2.1  bouyer 				FIFO_THRESH_80B | START_CTL_TH | READ_CMD_MRM);
    709  1.5.2.1  bouyer 		break;
    710  1.5.2.1  bouyer 	}
    711  1.5.2.1  bouyer 
    712  1.5.2.1  bouyer 	/*
    713  1.5.2.1  bouyer 	 * Microcode operating variables for WDTR, SDTR, and command tag
    714  1.5.2.1  bouyer 	 * queuing will be set in AdvInquiryHandling() based on what a
    715  1.5.2.1  bouyer 	 * device reports it is capable of in Inquiry byte 7.
    716  1.5.2.1  bouyer 	 *
    717  1.5.2.1  bouyer 	 * If SCSI Bus Resets have been disabled, then directly set
    718  1.5.2.1  bouyer 	 * SDTR and WDTR from the EEPROM configuration. This will allow
    719  1.5.2.1  bouyer 	 * the BIOS and warm boot to work without a SCSI bus hang on
    720  1.5.2.1  bouyer 	 * the Inquiry caused by host and target mismatched DTR values.
    721  1.5.2.1  bouyer 	 * Without the SCSI Bus Reset, before an Inquiry a device can't
    722  1.5.2.1  bouyer 	 * be assumed to be in Asynchronous, Narrow mode.
    723  1.5.2.1  bouyer 	 */
    724  1.5.2.1  bouyer 	if ((sc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS) == 0) {
    725  1.5.2.1  bouyer 		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE, sc->wdtr_able);
    726  1.5.2.1  bouyer 		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, sc->sdtr_able);
    727  1.5.2.1  bouyer 	}
    728  1.5.2.1  bouyer 
    729  1.5.2.1  bouyer 	/*
    730  1.5.2.1  bouyer 	 * Set microcode operating variables for SDTR_SPEED1, SDTR_SPEED2,
    731  1.5.2.1  bouyer 	 * SDTR_SPEED3, and SDTR_SPEED4 based on the ULTRA EEPROM per TID
    732  1.5.2.1  bouyer 	 * bitmask. These values determine the maximum SDTR speed negotiated
    733  1.5.2.1  bouyer 	 * with a device.
    734  1.5.2.1  bouyer 	 *
    735  1.5.2.1  bouyer 	 * The SDTR per TID bitmask overrides the SDTR_SPEED1, SDTR_SPEED2,
    736  1.5.2.1  bouyer 	 * SDTR_SPEED3, and SDTR_SPEED4 values so it is safe to set them
    737  1.5.2.1  bouyer 	 * without determining here whether the device supports SDTR.
    738  1.5.2.1  bouyer 	 */
    739  1.5.2.1  bouyer 	switch (sc->chip_type) {
    740  1.5.2.1  bouyer 	case ADW_CHIP_ASC3550:
    741  1.5.2.1  bouyer 		word = 0;
    742  1.5.2.1  bouyer 		for (tid = 0; tid <= ADW_MAX_TID; tid++) {
    743  1.5.2.1  bouyer 			if (ADW_TID_TO_TIDMASK(tid) & sc->ultra_able) {
    744  1.5.2.1  bouyer 				/* Set Ultra speed for TID 'tid'. */
    745  1.5.2.1  bouyer 				word |= (0x3 << (4 * (tid % 4)));
    746  1.5.2.1  bouyer 			} else {
    747  1.5.2.1  bouyer 				/* Set Fast speed for TID 'tid'. */
    748  1.5.2.1  bouyer 				word |= (0x2 << (4 * (tid % 4)));
    749  1.5.2.1  bouyer 			}
    750  1.5.2.1  bouyer 			/* Check if done with sdtr_speed1. */
    751  1.5.2.1  bouyer 			if (tid == 3) {
    752  1.5.2.1  bouyer 				ADW_WRITE_WORD_LRAM(iot, ioh,
    753  1.5.2.1  bouyer 						ADW_MC_SDTR_SPEED1, word);
    754  1.5.2.1  bouyer 				word = 0;
    755  1.5.2.1  bouyer 			/* Check if done with sdtr_speed2. */
    756  1.5.2.1  bouyer 			} else if (tid == 7) {
    757  1.5.2.1  bouyer 				ADW_WRITE_WORD_LRAM(iot, ioh,
    758  1.5.2.1  bouyer 						ADW_MC_SDTR_SPEED2, word);
    759  1.5.2.1  bouyer 				word = 0;
    760  1.5.2.1  bouyer 			/* Check if done with sdtr_speed3. */
    761  1.5.2.1  bouyer 			} else if (tid == 11) {
    762  1.5.2.1  bouyer 				ADW_WRITE_WORD_LRAM(iot, ioh,
    763  1.5.2.1  bouyer 						ADW_MC_SDTR_SPEED3, word);
    764  1.5.2.1  bouyer 				word = 0;
    765  1.5.2.1  bouyer 			/* Check if done with sdtr_speed4. */
    766  1.5.2.1  bouyer 			} else if (tid == 15) {
    767  1.5.2.1  bouyer 				ADW_WRITE_WORD_LRAM(iot, ioh,
    768  1.5.2.1  bouyer 						ADW_MC_SDTR_SPEED4, word);
    769  1.5.2.1  bouyer 				/* End of loop. */
    770  1.5.2.1  bouyer 			}
    771  1.5.2.1  bouyer 		}
    772  1.5.2.1  bouyer 
    773  1.5.2.1  bouyer 		/*
    774  1.5.2.1  bouyer 		 * Set microcode operating variable for the
    775  1.5.2.1  bouyer 		 * disconnect per TID bitmask.
    776  1.5.2.1  bouyer 		 */
    777  1.5.2.1  bouyer 		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DISC_ENABLE,
    778  1.5.2.1  bouyer 							sc->cfg.disc_enable);
    779  1.5.2.1  bouyer 		break;
    780  1.5.2.1  bouyer 
    781  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C0800:
    782  1.5.2.1  bouyer 		/* FALLTHROUGH */
    783  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C1600:
    784  1.5.2.1  bouyer 		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DISC_ENABLE,
    785  1.5.2.1  bouyer 							sc->cfg.disc_enable);
    786  1.5.2.1  bouyer 		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_SPEED1,
    787  1.5.2.1  bouyer 							sc->sdtr_speed1);
    788  1.5.2.1  bouyer 		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_SPEED2,
    789  1.5.2.1  bouyer 							sc->sdtr_speed2);
    790  1.5.2.1  bouyer 		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_SPEED3,
    791  1.5.2.1  bouyer 							sc->sdtr_speed3);
    792  1.5.2.1  bouyer 		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_SPEED4,
    793  1.5.2.1  bouyer 							sc->sdtr_speed4);
    794  1.5.2.1  bouyer 		break;
    795      1.1   dante 	}
    796  1.5.2.1  bouyer 
    797  1.5.2.1  bouyer 
    798      1.1   dante 	/*
    799  1.5.2.1  bouyer 	 * Set SCSI_CFG0 Microcode Default Value.
    800      1.1   dante 	 *
    801  1.5.2.1  bouyer 	 * The microcode will set the SCSI_CFG0 register using this value
    802  1.5.2.1  bouyer 	 * after it is started below.
    803      1.1   dante 	 */
    804  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_SCSI_CFG0,
    805  1.5.2.1  bouyer 		ADW_PARITY_EN | ADW_QUEUE_128 | ADW_SEL_TMO_LONG |
    806  1.5.2.1  bouyer 		ADW_OUR_ID_EN | sc->chip_scsi_id);
    807  1.5.2.1  bouyer 
    808  1.5.2.1  bouyer 
    809  1.5.2.1  bouyer 	switch(sc->chip_type) {
    810  1.5.2.1  bouyer 	case ADW_CHIP_ASC3550:
    811  1.5.2.1  bouyer 		error_code = AdwASC3550Cabling(iot, ioh, &sc->cfg);
    812  1.5.2.1  bouyer 		break;
    813  1.5.2.1  bouyer 
    814  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C0800:
    815  1.5.2.1  bouyer 		error_code = AdwASC38C0800Cabling(iot, ioh, &sc->cfg);
    816  1.5.2.1  bouyer 		break;
    817  1.5.2.1  bouyer 
    818  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C1600:
    819  1.5.2.1  bouyer 		error_code = AdwASC38C1600Cabling(iot, ioh, &sc->cfg);
    820  1.5.2.1  bouyer 		break;
    821  1.5.2.1  bouyer 	}
    822  1.5.2.1  bouyer 	if(error_code) {
    823  1.5.2.1  bouyer 		return error_code;
    824  1.5.2.1  bouyer 	}
    825      1.1   dante 
    826      1.1   dante 	/*
    827  1.5.2.1  bouyer 	 * Set SEL_MASK Microcode Default Value
    828      1.2   dante 	 *
    829  1.5.2.1  bouyer 	 * The microcode will set the SEL_MASK register using this value
    830  1.5.2.1  bouyer 	 * after it is started below.
    831      1.1   dante 	 */
    832  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_SEL_MASK,
    833  1.5.2.1  bouyer 		ADW_TID_TO_TIDMASK(sc->chip_scsi_id));
    834      1.1   dante 
    835  1.5.2.1  bouyer 	/*
    836  1.5.2.1  bouyer 	 * Create and Initialize Host->RISC Carrier lists
    837  1.5.2.1  bouyer 	 */
    838  1.5.2.1  bouyer 	sc->carr_freelist = AdwInitCarriers(sc->sc_dmamap_carrier,
    839  1.5.2.1  bouyer 						sc->sc_control->carriers);
    840      1.1   dante 
    841      1.1   dante 	/*
    842  1.5.2.1  bouyer 	 * Set-up the Host->RISC Initiator Command Queue (ICQ).
    843  1.5.2.1  bouyer 	 */
    844  1.5.2.1  bouyer 
    845  1.5.2.1  bouyer 	if ((sc->icq_sp = sc->carr_freelist) == NULL) {
    846  1.5.2.1  bouyer 		return ADW_IERR_NO_CARRIER;
    847  1.5.2.1  bouyer 	}
    848  1.5.2.1  bouyer 	sc->carr_freelist = ADW_CARRIER_VADDR(sc,
    849  1.5.2.1  bouyer 			ASC_GET_CARRP(sc->icq_sp->next_ba));
    850  1.5.2.1  bouyer 
    851  1.5.2.1  bouyer 	/*
    852  1.5.2.1  bouyer 	 * The first command issued will be placed in the stopper carrier.
    853  1.5.2.1  bouyer 	 */
    854  1.5.2.1  bouyer 	sc->icq_sp->next_ba = ASC_CQ_STOPPER;
    855  1.5.2.1  bouyer 
    856  1.5.2.1  bouyer 	/*
    857  1.5.2.1  bouyer 	 * Set RISC ICQ physical address start value.
    858  1.5.2.1  bouyer 	 */
    859  1.5.2.1  bouyer 	ADW_WRITE_DWORD_LRAM(iot, ioh, ADW_MC_ICQ, sc->icq_sp->carr_ba);
    860  1.5.2.1  bouyer 
    861  1.5.2.1  bouyer 	/*
    862  1.5.2.1  bouyer 	 * Initialize the COMMA register to the same value otherwise
    863  1.5.2.1  bouyer 	 * the RISC will prematurely detect a command is available.
    864  1.5.2.1  bouyer 	 */
    865  1.5.2.1  bouyer 	if(sc->chip_type == ADW_CHIP_ASC38C1600) {
    866  1.5.2.1  bouyer 		ADW_WRITE_DWORD_REGISTER(iot, ioh, IOPDW_COMMA,
    867  1.5.2.1  bouyer 							sc->icq_sp->carr_ba);
    868  1.5.2.1  bouyer 	}
    869  1.5.2.1  bouyer 
    870  1.5.2.1  bouyer 	/*
    871  1.5.2.1  bouyer 	 * Set-up the RISC->Host Initiator Response Queue (IRQ).
    872  1.5.2.1  bouyer 	 */
    873  1.5.2.1  bouyer 	if ((sc->irq_sp = sc->carr_freelist) == NULL) {
    874  1.5.2.1  bouyer 		return ADW_IERR_NO_CARRIER;
    875  1.5.2.1  bouyer 	}
    876  1.5.2.1  bouyer 	sc->carr_freelist = ADW_CARRIER_VADDR(sc,
    877  1.5.2.1  bouyer 			ASC_GET_CARRP(sc->irq_sp->next_ba));
    878  1.5.2.1  bouyer 
    879  1.5.2.1  bouyer 	/*
    880  1.5.2.1  bouyer 	 * The first command completed by the RISC will be placed in
    881  1.5.2.1  bouyer 	 * the stopper.
    882      1.1   dante 	 *
    883  1.5.2.1  bouyer 	 * Note: Set 'next_ba' to ASC_CQ_STOPPER. When the request is
    884  1.5.2.1  bouyer 	 * completed the RISC will set the ASC_RQ_DONE bit.
    885  1.5.2.1  bouyer 	 */
    886  1.5.2.1  bouyer 	sc->irq_sp->next_ba = ASC_CQ_STOPPER;
    887  1.5.2.1  bouyer 
    888  1.5.2.1  bouyer 	/*
    889  1.5.2.1  bouyer 	 * Set RISC IRQ physical address start value.
    890  1.5.2.1  bouyer 	 */
    891  1.5.2.1  bouyer 	ADW_WRITE_DWORD_LRAM(iot, ioh, ADW_MC_IRQ, sc->irq_sp->carr_ba);
    892  1.5.2.1  bouyer 	sc->carr_pending_cnt = 0;
    893  1.5.2.1  bouyer 
    894  1.5.2.1  bouyer 	ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_INTR_ENABLES,
    895  1.5.2.1  bouyer 		(ADW_INTR_ENABLE_HOST_INTR | ADW_INTR_ENABLE_GLOBAL_INTR));
    896  1.5.2.1  bouyer 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_CODE_BEGIN_ADDR, word);
    897  1.5.2.1  bouyer 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_PC, word);
    898  1.5.2.1  bouyer 
    899  1.5.2.1  bouyer 	/* finally, finally, gentlemen, start your engine */
    900  1.5.2.1  bouyer 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_RISC_CSR, ADW_RISC_CSR_RUN);
    901  1.5.2.1  bouyer 
    902  1.5.2.1  bouyer 	/*
    903  1.5.2.1  bouyer 	 * Reset the SCSI Bus if the EEPROM indicates that SCSI Bus
    904  1.5.2.1  bouyer 	 * Resets should be performed. The RISC has to be running
    905  1.5.2.1  bouyer 	 * to issue a SCSI Bus Reset.
    906  1.5.2.1  bouyer 	 */
    907  1.5.2.1  bouyer 	if (sc->bios_ctrl & BIOS_CTRL_RESET_SCSI_BUS)
    908  1.5.2.1  bouyer 	{
    909  1.5.2.1  bouyer 		/*
    910  1.5.2.1  bouyer 		 * If the BIOS Signature is present in memory, restore the
    911  1.5.2.1  bouyer 		 * BIOS Handshake Configuration Table and do not perform
    912  1.5.2.1  bouyer 		 * a SCSI Bus Reset.
    913  1.5.2.1  bouyer 		 */
    914  1.5.2.1  bouyer 		if (bios_mem[(ADW_MC_BIOS_SIGNATURE - ADW_MC_BIOSMEM)/2] ==
    915  1.5.2.1  bouyer 				0x55AA) {
    916  1.5.2.1  bouyer 			/*
    917  1.5.2.1  bouyer 			 * Restore per TID negotiated values.
    918  1.5.2.1  bouyer 			 */
    919  1.5.2.1  bouyer 			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE,
    920  1.5.2.1  bouyer 					wdtr_able);
    921  1.5.2.1  bouyer 			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE,
    922  1.5.2.1  bouyer 					sdtr_able);
    923  1.5.2.1  bouyer 			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE,
    924  1.5.2.1  bouyer 					tagqng_able);
    925  1.5.2.1  bouyer 			for (tid = 0; tid <= ADW_MAX_TID; tid++) {
    926  1.5.2.1  bouyer 				ADW_WRITE_BYTE_LRAM(iot, ioh,
    927  1.5.2.1  bouyer 						ADW_MC_NUMBER_OF_MAX_CMD + tid,
    928  1.5.2.1  bouyer 						max_cmd[tid]);
    929  1.5.2.1  bouyer 			}
    930  1.5.2.1  bouyer 		} else {
    931  1.5.2.1  bouyer 			if (AdwResetCCB(sc) != ADW_TRUE) {
    932  1.5.2.1  bouyer 				error_code = ADW_WARN_BUSRESET_ERROR;
    933  1.5.2.1  bouyer 			}
    934  1.5.2.1  bouyer 		}
    935  1.5.2.1  bouyer 	}
    936  1.5.2.1  bouyer 
    937  1.5.2.1  bouyer 	return error_code;
    938  1.5.2.1  bouyer }
    939  1.5.2.1  bouyer 
    940  1.5.2.1  bouyer 
    941  1.5.2.1  bouyer int
    942  1.5.2.1  bouyer AdwRamSelfTest(iot, ioh, chip_type)
    943  1.5.2.1  bouyer 	bus_space_tag_t iot;
    944  1.5.2.1  bouyer 	bus_space_handle_t ioh;
    945  1.5.2.1  bouyer 	u_int8_t chip_type;
    946  1.5.2.1  bouyer {
    947  1.5.2.1  bouyer 	int		i;
    948  1.5.2.1  bouyer 	u_int8_t	byte;
    949  1.5.2.1  bouyer 
    950  1.5.2.1  bouyer 
    951  1.5.2.1  bouyer 	if ((chip_type == ADW_CHIP_ASC38C0800) ||
    952  1.5.2.1  bouyer 	    (chip_type == ADW_CHIP_ASC38C1600)) {
    953  1.5.2.1  bouyer 		/*
    954  1.5.2.1  bouyer 		 * RAM BIST (RAM Built-In Self Test)
    955  1.5.2.1  bouyer 		 *
    956  1.5.2.1  bouyer 		 * Address : I/O base + offset 0x38h register (byte).
    957  1.5.2.1  bouyer 		 * Function: Bit 7-6(RW) : RAM mode
    958  1.5.2.1  bouyer 		 *			    Normal Mode   : 0x00
    959  1.5.2.1  bouyer 		 *			    Pre-test Mode : 0x40
    960  1.5.2.1  bouyer 		 *			    RAM Test Mode : 0x80
    961  1.5.2.1  bouyer 		 *	     Bit 5	 : unused
    962  1.5.2.1  bouyer 		 *	     Bit 4(RO)   : Done bit
    963  1.5.2.1  bouyer 		 *	     Bit 3-0(RO) : Status
    964  1.5.2.1  bouyer 		 *			    Host Error    : 0x08
    965  1.5.2.1  bouyer 		 *			    Int_RAM Error : 0x04
    966  1.5.2.1  bouyer 		 *			    RISC Error    : 0x02
    967  1.5.2.1  bouyer 		 *			    SCSI Error    : 0x01
    968  1.5.2.1  bouyer 		 *			    No Error	  : 0x00
    969  1.5.2.1  bouyer 		 *
    970  1.5.2.1  bouyer 		 * Note: RAM BIST code should be put right here, before loading
    971  1.5.2.1  bouyer 		 * the microcode and after saving the RISC memory BIOS region.
    972  1.5.2.1  bouyer 		 */
    973  1.5.2.1  bouyer 
    974  1.5.2.1  bouyer 		/*
    975  1.5.2.1  bouyer 		 * LRAM Pre-test
    976  1.5.2.1  bouyer 		 *
    977  1.5.2.1  bouyer 		 * Write PRE_TEST_MODE (0x40) to register and wait for
    978  1.5.2.1  bouyer 		 * 10 milliseconds.
    979  1.5.2.1  bouyer 		 * If Done bit not set or low nibble not PRE_TEST_VALUE (0x05),
    980  1.5.2.1  bouyer 		 * return an error. Reset to NORMAL_MODE (0x00) and do again.
    981  1.5.2.1  bouyer 		 * If cannot reset to NORMAL_MODE, return an error too.
    982  1.5.2.1  bouyer 		 */
    983  1.5.2.1  bouyer 		for (i = 0; i < 2; i++) {
    984  1.5.2.1  bouyer 			ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST,
    985  1.5.2.1  bouyer 					PRE_TEST_MODE);
    986  1.5.2.1  bouyer 			 /* Wait for 10ms before reading back. */
    987  1.5.2.1  bouyer 			AdwSleepMilliSecond(10);
    988  1.5.2.1  bouyer 			byte = ADW_READ_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST);
    989  1.5.2.1  bouyer 			if ((byte & RAM_TEST_DONE) == 0 || (byte & 0x0F) !=
    990  1.5.2.1  bouyer 					PRE_TEST_VALUE) {
    991  1.5.2.1  bouyer 				return ADW_IERR_BIST_PRE_TEST;
    992  1.5.2.1  bouyer 			}
    993  1.5.2.1  bouyer 
    994  1.5.2.1  bouyer 			ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST,
    995  1.5.2.1  bouyer 								NORMAL_MODE);
    996  1.5.2.1  bouyer 			/* Wait for 10ms before reading back. */
    997  1.5.2.1  bouyer 			AdwSleepMilliSecond(10);
    998  1.5.2.1  bouyer 			if (ADW_READ_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST)
    999  1.5.2.1  bouyer 			    != NORMAL_VALUE) {
   1000  1.5.2.1  bouyer 				return ADW_IERR_BIST_PRE_TEST;
   1001  1.5.2.1  bouyer 			}
   1002  1.5.2.1  bouyer 		}
   1003  1.5.2.1  bouyer 
   1004  1.5.2.1  bouyer 		/*
   1005  1.5.2.1  bouyer 		 * LRAM Test - It takes about 1.5 ms to run through the test.
   1006  1.5.2.1  bouyer 		 *
   1007  1.5.2.1  bouyer 		 * Write RAM_TEST_MODE (0x80) to register and wait for
   1008  1.5.2.1  bouyer 		 * 10 milliseconds.
   1009  1.5.2.1  bouyer 		 * If Done bit not set or Status not 0, save register byte,
   1010  1.5.2.1  bouyer 		 * set the err_code, and return an error.
   1011  1.5.2.1  bouyer 		 */
   1012  1.5.2.1  bouyer 		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST, RAM_TEST_MODE);
   1013  1.5.2.1  bouyer 		/* Wait for 10ms before checking status. */
   1014  1.5.2.1  bouyer 		AdwSleepMilliSecond(10);
   1015  1.5.2.1  bouyer 
   1016  1.5.2.1  bouyer 		byte = ADW_READ_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST);
   1017  1.5.2.1  bouyer 		if ((byte & RAM_TEST_DONE)==0 || (byte & RAM_TEST_STATUS)!=0) {
   1018  1.5.2.1  bouyer 			/* Get here if Done bit not set or Status not 0. */
   1019  1.5.2.1  bouyer 			return ADW_IERR_BIST_RAM_TEST;
   1020  1.5.2.1  bouyer 		}
   1021  1.5.2.1  bouyer 
   1022  1.5.2.1  bouyer 		/* We need to reset back to normal mode after LRAM test passes*/
   1023  1.5.2.1  bouyer 		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_RAM_BIST, NORMAL_MODE);
   1024  1.5.2.1  bouyer 	}
   1025  1.5.2.1  bouyer 
   1026  1.5.2.1  bouyer 	return 0;
   1027  1.5.2.1  bouyer }
   1028  1.5.2.1  bouyer 
   1029  1.5.2.1  bouyer 
   1030  1.5.2.1  bouyer int
   1031  1.5.2.1  bouyer AdwLoadMCode(iot, ioh, bios_mem, chip_type)
   1032  1.5.2.1  bouyer 	bus_space_tag_t iot;
   1033  1.5.2.1  bouyer 	bus_space_handle_t ioh;
   1034  1.5.2.1  bouyer 	u_int16_t *bios_mem;
   1035  1.5.2.1  bouyer 	u_int8_t chip_type;
   1036  1.5.2.1  bouyer {
   1037  1.5.2.1  bouyer 	u_int8_t	*mcode_data;
   1038  1.5.2.1  bouyer 	u_int32_t	 mcode_chksum;
   1039  1.5.2.1  bouyer 	u_int16_t	 mcode_size;
   1040  1.5.2.1  bouyer 	u_int32_t	sum;
   1041  1.5.2.1  bouyer 	u_int16_t	code_sum;
   1042  1.5.2.1  bouyer 	int		begin_addr;
   1043  1.5.2.1  bouyer 	int		end_addr;
   1044  1.5.2.1  bouyer 	int		word;
   1045  1.5.2.1  bouyer 	int		adw_memsize;
   1046  1.5.2.1  bouyer 	int		adw_mcode_expanded_size;
   1047  1.5.2.1  bouyer 	int		i, j;
   1048  1.5.2.1  bouyer 
   1049  1.5.2.1  bouyer 
   1050  1.5.2.1  bouyer 	switch(chip_type) {
   1051  1.5.2.1  bouyer 	case ADW_CHIP_ASC3550:
   1052  1.5.2.1  bouyer 		mcode_data = (u_int8_t *)adw_asc3550_mcode_data.mcode_data;
   1053  1.5.2.1  bouyer 		mcode_chksum = (u_int32_t)adw_asc3550_mcode_data.mcode_chksum;
   1054  1.5.2.1  bouyer 		mcode_size = (u_int16_t)adw_asc3550_mcode_data.mcode_size;
   1055  1.5.2.1  bouyer 		adw_memsize = ADW_3550_MEMSIZE;
   1056  1.5.2.1  bouyer 		break;
   1057  1.5.2.1  bouyer 
   1058  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C0800:
   1059  1.5.2.1  bouyer 		mcode_data = (u_int8_t *)adw_asc38C0800_mcode_data.mcode_data;
   1060  1.5.2.1  bouyer 		mcode_chksum =(u_int32_t)adw_asc38C0800_mcode_data.mcode_chksum;
   1061  1.5.2.1  bouyer 		mcode_size = (u_int16_t)adw_asc38C0800_mcode_data.mcode_size;
   1062  1.5.2.1  bouyer 		adw_memsize = ADW_38C0800_MEMSIZE;
   1063  1.5.2.1  bouyer 		break;
   1064  1.5.2.1  bouyer 
   1065  1.5.2.1  bouyer 	case ADW_CHIP_ASC38C1600:
   1066  1.5.2.1  bouyer 		mcode_data = (u_int8_t *)adw_asc38C1600_mcode_data.mcode_data;
   1067  1.5.2.1  bouyer 		mcode_chksum =(u_int32_t)adw_asc38C1600_mcode_data.mcode_chksum;
   1068  1.5.2.1  bouyer 		mcode_size = (u_int16_t)adw_asc38C1600_mcode_data.mcode_size;
   1069  1.5.2.1  bouyer 		adw_memsize = ADW_38C1600_MEMSIZE;
   1070  1.5.2.1  bouyer 		break;
   1071  1.5.2.1  bouyer 	}
   1072  1.5.2.1  bouyer 
   1073  1.5.2.1  bouyer 	/*
   1074  1.5.2.1  bouyer 	 * Write the microcode image to RISC memory starting at address 0.
   1075  1.5.2.1  bouyer 	 */
   1076  1.5.2.1  bouyer 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_RAM_ADDR, 0);
   1077  1.5.2.1  bouyer 
   1078  1.5.2.1  bouyer 	/* Assume the following compressed format of the microcode buffer:
   1079  1.5.2.1  bouyer 	 *
   1080  1.5.2.1  bouyer 	 *  254 word (508 byte) table indexed by byte code followed
   1081  1.5.2.1  bouyer 	 *  by the following byte codes:
   1082  1.5.2.1  bouyer 	 *
   1083  1.5.2.1  bouyer 	 *    1-Byte Code:
   1084  1.5.2.1  bouyer 	 *	00: Emit word 0 in table.
   1085  1.5.2.1  bouyer 	 *	01: Emit word 1 in table.
   1086  1.5.2.1  bouyer 	 *	.
   1087  1.5.2.1  bouyer 	 *	FD: Emit word 253 in table.
   1088  1.5.2.1  bouyer 	 *
   1089  1.5.2.1  bouyer 	 *    Multi-Byte Code:
   1090  1.5.2.1  bouyer 	 *	FE WW WW: (3 byte code) Word to emit is the next word WW WW.
   1091  1.5.2.1  bouyer 	 *	FF BB WW WW: (4 byte code) Emit BB count times next word WW WW.
   1092  1.5.2.1  bouyer 	 */
   1093  1.5.2.1  bouyer 	word = 0;
   1094  1.5.2.1  bouyer 	for (i = 253 * 2; i < mcode_size; i++) {
   1095  1.5.2.1  bouyer 		if (mcode_data[i] == 0xff) {
   1096  1.5.2.1  bouyer 			for (j = 0; j < mcode_data[i + 1]; j++) {
   1097  1.5.2.1  bouyer 				ADW_WRITE_WORD_AUTO_INC_LRAM(iot, ioh,
   1098  1.5.2.1  bouyer 				  (((u_int16_t)mcode_data[i + 3] << 8) |
   1099  1.5.2.1  bouyer 				  mcode_data[i + 2]));
   1100  1.5.2.1  bouyer 				word++;
   1101  1.5.2.1  bouyer 			}
   1102  1.5.2.1  bouyer 			i += 3;
   1103  1.5.2.1  bouyer 		} else if (mcode_data[i] == 0xfe) {
   1104  1.5.2.1  bouyer 			ADW_WRITE_WORD_AUTO_INC_LRAM(iot, ioh,
   1105  1.5.2.1  bouyer 			    (((u_int16_t)mcode_data[i + 2] << 8) |
   1106  1.5.2.1  bouyer 			    mcode_data[i + 1]));
   1107  1.5.2.1  bouyer 			i += 2;
   1108  1.5.2.1  bouyer 			word++;
   1109  1.5.2.1  bouyer 		} else {
   1110  1.5.2.1  bouyer 			ADW_WRITE_WORD_AUTO_INC_LRAM(iot, ioh, (((u_int16_t)
   1111  1.5.2.1  bouyer 			 mcode_data[(mcode_data[i] * 2) + 1] <<8) |
   1112  1.5.2.1  bouyer 			 mcode_data[mcode_data[i] * 2]));
   1113  1.5.2.1  bouyer 			word++;
   1114  1.5.2.1  bouyer 		}
   1115  1.5.2.1  bouyer 	}
   1116  1.5.2.1  bouyer 
   1117  1.5.2.1  bouyer 	/*
   1118  1.5.2.1  bouyer 	 * Set 'word' for later use to clear the rest of memory and save
   1119  1.5.2.1  bouyer 	 * the expanded mcode size.
   1120  1.5.2.1  bouyer 	 */
   1121  1.5.2.1  bouyer 	word *= 2;
   1122  1.5.2.1  bouyer 	adw_mcode_expanded_size = word;
   1123  1.5.2.1  bouyer 
   1124  1.5.2.1  bouyer 	/*
   1125  1.5.2.1  bouyer 	 * Clear the rest of the Internal RAM.
   1126  1.5.2.1  bouyer 	 */
   1127  1.5.2.1  bouyer 	for (; word < adw_memsize; word += 2) {
   1128  1.5.2.1  bouyer 		ADW_WRITE_WORD_AUTO_INC_LRAM(iot, ioh, 0);
   1129  1.5.2.1  bouyer 	}
   1130  1.5.2.1  bouyer 
   1131  1.5.2.1  bouyer 	/*
   1132  1.5.2.1  bouyer 	 * Verify the microcode checksum.
   1133  1.5.2.1  bouyer 	 */
   1134  1.5.2.1  bouyer 	sum = 0;
   1135  1.5.2.1  bouyer 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_RAM_ADDR, 0);
   1136  1.5.2.1  bouyer 
   1137  1.5.2.1  bouyer 	for (word = 0; word < adw_mcode_expanded_size; word += 2) {
   1138  1.5.2.1  bouyer 		sum += ADW_READ_WORD_AUTO_INC_LRAM(iot, ioh);
   1139  1.5.2.1  bouyer 	}
   1140  1.5.2.1  bouyer 
   1141  1.5.2.1  bouyer 	if (sum != mcode_chksum) {
   1142  1.5.2.1  bouyer 		return ADW_IERR_MCODE_CHKSUM;
   1143  1.5.2.1  bouyer 	}
   1144  1.5.2.1  bouyer 
   1145  1.5.2.1  bouyer 	/*
   1146  1.5.2.1  bouyer 	 * Restore the RISC memory BIOS region.
   1147  1.5.2.1  bouyer 	 */
   1148  1.5.2.1  bouyer 	for (i = 0; i < ADW_MC_BIOSLEN/2; i++) {
   1149  1.5.2.1  bouyer 		if(chip_type == ADW_CHIP_ASC3550) {
   1150  1.5.2.1  bouyer 			ADW_WRITE_BYTE_LRAM(iot, ioh, ADW_MC_BIOSMEM + (2 * i),
   1151  1.5.2.1  bouyer 								bios_mem[i]);
   1152  1.5.2.1  bouyer 		} else {
   1153  1.5.2.1  bouyer 			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_BIOSMEM + (2 * i),
   1154  1.5.2.1  bouyer 								bios_mem[i]);
   1155  1.5.2.1  bouyer 		}
   1156  1.5.2.1  bouyer 	}
   1157  1.5.2.1  bouyer 
   1158  1.5.2.1  bouyer 	/*
   1159  1.5.2.1  bouyer 	 * Calculate and write the microcode code checksum to the microcode
   1160  1.5.2.1  bouyer 	 * code checksum location ADW_MC_CODE_CHK_SUM (0x2C).
   1161  1.5.2.1  bouyer 	 */
   1162  1.5.2.1  bouyer 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_CODE_BEGIN_ADDR, begin_addr);
   1163  1.5.2.1  bouyer 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_CODE_END_ADDR, end_addr);
   1164  1.5.2.1  bouyer 	code_sum = 0;
   1165  1.5.2.1  bouyer 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_RAM_ADDR, begin_addr);
   1166  1.5.2.1  bouyer 	for (word = begin_addr; word < end_addr; word += 2) {
   1167  1.5.2.1  bouyer 		code_sum += ADW_READ_WORD_AUTO_INC_LRAM(iot, ioh);
   1168  1.5.2.1  bouyer 	}
   1169  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_CODE_CHK_SUM, code_sum);
   1170  1.5.2.1  bouyer 
   1171  1.5.2.1  bouyer 	/*
   1172  1.5.2.1  bouyer 	 * Set the chip type.
   1173      1.1   dante 	 */
   1174  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_CHIP_TYPE, chip_type);
   1175  1.5.2.1  bouyer 
   1176  1.5.2.1  bouyer 	return 0;
   1177  1.5.2.1  bouyer }
   1178  1.5.2.1  bouyer 
   1179  1.5.2.1  bouyer 
   1180  1.5.2.1  bouyer int
   1181  1.5.2.1  bouyer AdwASC3550Cabling(iot, ioh, cfg)
   1182  1.5.2.1  bouyer 	bus_space_tag_t iot;
   1183  1.5.2.1  bouyer 	bus_space_handle_t ioh;
   1184  1.5.2.1  bouyer 	ADW_DVC_CFG *cfg;
   1185  1.5.2.1  bouyer {
   1186  1.5.2.1  bouyer 	u_int16_t	scsi_cfg1;
   1187  1.5.2.1  bouyer 
   1188      1.2   dante 
   1189      1.1   dante 	/*
   1190      1.1   dante 	 * Determine SCSI_CFG1 Microcode Default Value.
   1191      1.1   dante 	 *
   1192      1.1   dante 	 * The microcode will set the SCSI_CFG1 register using this value
   1193      1.1   dante 	 * after it is started below.
   1194      1.1   dante 	 */
   1195      1.1   dante 
   1196      1.1   dante 	/* Read current SCSI_CFG1 Register value. */
   1197      1.1   dante 	scsi_cfg1 = ADW_READ_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1);
   1198      1.1   dante 
   1199      1.1   dante 	/*
   1200  1.5.2.1  bouyer 	 * If all three connectors are in use in ASC3550, return an error.
   1201      1.1   dante 	 */
   1202      1.1   dante 	if ((scsi_cfg1 & CABLE_ILLEGAL_A) == 0 ||
   1203  1.5.2.1  bouyer 	     (scsi_cfg1 & CABLE_ILLEGAL_B) == 0) {
   1204  1.5.2.1  bouyer 		return ADW_IERR_ILLEGAL_CONNECTION;
   1205      1.1   dante 	}
   1206  1.5.2.1  bouyer 
   1207      1.1   dante 	/*
   1208  1.5.2.1  bouyer 	 * If the cable is reversed all of the SCSI_CTRL register signals
   1209  1.5.2.1  bouyer 	 * will be set. Check for and return an error if this condition is
   1210  1.5.2.1  bouyer 	 * found.
   1211      1.1   dante 	 */
   1212  1.5.2.1  bouyer 	if ((ADW_READ_WORD_REGISTER(iot,ioh, IOPW_SCSI_CTRL) & 0x3F07)==0x3F07){
   1213  1.5.2.1  bouyer 		return ADW_IERR_REVERSED_CABLE;
   1214      1.2   dante 	}
   1215      1.1   dante 
   1216      1.1   dante 	/*
   1217      1.1   dante 	 * If this is a differential board and a single-ended device
   1218      1.1   dante 	 * is attached to one of the connectors, return an error.
   1219      1.1   dante 	 */
   1220  1.5.2.1  bouyer 	if ((scsi_cfg1 & ADW_DIFF_MODE) &&
   1221  1.5.2.1  bouyer 	    (scsi_cfg1 & ADW_DIFF_SENSE) == 0) {
   1222  1.5.2.1  bouyer 		return ADW_IERR_SINGLE_END_DEVICE;
   1223  1.5.2.1  bouyer 	}
   1224      1.1   dante 
   1225      1.1   dante 	/*
   1226      1.1   dante 	 * If automatic termination control is enabled, then set the
   1227  1.5.2.1  bouyer 	 * termination value based on a table listed in a_condor.h.
   1228      1.1   dante 	 *
   1229      1.1   dante 	 * If manual termination was specified with an EEPROM setting
   1230  1.5.2.1  bouyer 	 * then 'termination' was set-up in AdwInitFromEEPROM() and
   1231      1.1   dante 	 * is ready to be 'ored' into SCSI_CFG1.
   1232      1.1   dante 	 */
   1233  1.5.2.1  bouyer 	if (cfg->termination == 0) {
   1234      1.1   dante 		/*
   1235      1.2   dante 		 * The software always controls termination by setting
   1236  1.5.2.1  bouyer 		 * TERM_CTL_SEL.
   1237  1.5.2.1  bouyer 		 * If TERM_CTL_SEL were set to 0, the hardware would set
   1238  1.5.2.1  bouyer 		 * termination.
   1239  1.5.2.1  bouyer 		 */
   1240  1.5.2.1  bouyer 		cfg->termination |= ADW_TERM_CTL_SEL;
   1241      1.1   dante 
   1242  1.5.2.1  bouyer 		switch(scsi_cfg1 & ADW_CABLE_DETECT) {
   1243  1.5.2.1  bouyer 			/* TERM_CTL_H: on, TERM_CTL_L: on */
   1244  1.5.2.1  bouyer 			case 0x3: case 0x7: case 0xB:
   1245  1.5.2.1  bouyer 			case 0xD: case 0xE: case 0xF:
   1246  1.5.2.1  bouyer 				cfg->termination |=
   1247  1.5.2.1  bouyer 				(ADW_TERM_CTL_H | ADW_TERM_CTL_L);
   1248  1.5.2.1  bouyer 				break;
   1249  1.5.2.1  bouyer 
   1250  1.5.2.1  bouyer 			/* TERM_CTL_H: on, TERM_CTL_L: off */
   1251  1.5.2.1  bouyer 			case 0x1: case 0x5: case 0x9:
   1252  1.5.2.1  bouyer 			case 0xA: case 0xC:
   1253  1.5.2.1  bouyer 				cfg->termination |= ADW_TERM_CTL_H;
   1254  1.5.2.1  bouyer 				break;
   1255  1.5.2.1  bouyer 
   1256  1.5.2.1  bouyer 			/* TERM_CTL_H: off, TERM_CTL_L: off */
   1257  1.5.2.1  bouyer 			case 0x2: case 0x6:
   1258  1.5.2.1  bouyer 				break;
   1259      1.1   dante 		}
   1260      1.1   dante 	}
   1261  1.5.2.1  bouyer 
   1262      1.1   dante 	/*
   1263  1.5.2.1  bouyer 	 * Clear any set TERM_CTL_H and TERM_CTL_L bits.
   1264      1.1   dante 	 */
   1265      1.1   dante 	scsi_cfg1 &= ~ADW_TERM_CTL;
   1266      1.1   dante 
   1267      1.1   dante 	/*
   1268  1.5.2.1  bouyer 	 * Invert the TERM_CTL_H and TERM_CTL_L bits and then
   1269  1.5.2.1  bouyer 	 * set 'scsi_cfg1'. The TERM_POL bit does not need to be
   1270      1.1   dante 	 * referenced, because the hardware internally inverts
   1271  1.5.2.1  bouyer 	 * the Termination High and Low bits if TERM_POL is set.
   1272      1.1   dante 	 */
   1273  1.5.2.1  bouyer 	scsi_cfg1 |= (ADW_TERM_CTL_SEL | (~cfg->termination & ADW_TERM_CTL));
   1274      1.1   dante 
   1275      1.1   dante 	/*
   1276      1.1   dante 	 * Set SCSI_CFG1 Microcode Default Value
   1277      1.1   dante 	 *
   1278      1.1   dante 	 * Set filter value and possibly modified termination control
   1279      1.1   dante 	 * bits in the Microcode SCSI_CFG1 Register Value.
   1280      1.1   dante 	 *
   1281      1.1   dante 	 * The microcode will set the SCSI_CFG1 register using this value
   1282      1.1   dante 	 * after it is started below.
   1283      1.1   dante 	 */
   1284  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_SCSI_CFG1,
   1285  1.5.2.1  bouyer 						ADW_FLTR_DISABLE | scsi_cfg1);
   1286      1.1   dante 
   1287      1.1   dante 	/*
   1288  1.5.2.1  bouyer 	 * Set MEM_CFG Microcode Default Value
   1289      1.1   dante 	 *
   1290  1.5.2.1  bouyer 	 * The microcode will set the MEM_CFG register using this value
   1291      1.1   dante 	 * after it is started below.
   1292  1.5.2.1  bouyer 	 *
   1293  1.5.2.1  bouyer 	 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
   1294  1.5.2.1  bouyer 	 * are defined.
   1295  1.5.2.1  bouyer 	 *
   1296  1.5.2.1  bouyer 	 * ASC-3550 has 8KB internal memory.
   1297      1.1   dante 	 */
   1298  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_MEM_CFG,
   1299  1.5.2.1  bouyer 						ADW_BIOS_EN | ADW_RAM_SZ_8KB);
   1300  1.5.2.1  bouyer 
   1301  1.5.2.1  bouyer 	return 0;
   1302  1.5.2.1  bouyer }
   1303  1.5.2.1  bouyer 
   1304  1.5.2.1  bouyer 
   1305  1.5.2.1  bouyer int
   1306  1.5.2.1  bouyer AdwASC38C0800Cabling(iot, ioh, cfg)
   1307  1.5.2.1  bouyer 	bus_space_tag_t iot;
   1308  1.5.2.1  bouyer 	bus_space_handle_t ioh;
   1309  1.5.2.1  bouyer 	ADW_DVC_CFG *cfg;
   1310  1.5.2.1  bouyer {
   1311  1.5.2.1  bouyer 	u_int16_t	scsi_cfg1;
   1312  1.5.2.1  bouyer 
   1313      1.1   dante 
   1314      1.1   dante 	/*
   1315  1.5.2.1  bouyer 	 * Determine SCSI_CFG1 Microcode Default Value.
   1316      1.1   dante 	 *
   1317  1.5.2.1  bouyer 	 * The microcode will set the SCSI_CFG1 register using this value
   1318  1.5.2.1  bouyer 	 * after it is started below.
   1319      1.1   dante 	 */
   1320  1.5.2.1  bouyer 
   1321  1.5.2.1  bouyer 	/* Read current SCSI_CFG1 Register value. */
   1322  1.5.2.1  bouyer 	scsi_cfg1 = ADW_READ_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1);
   1323  1.5.2.1  bouyer 
   1324  1.5.2.1  bouyer 	/*
   1325  1.5.2.1  bouyer 	 * If the cable is reversed all of the SCSI_CTRL register signals
   1326  1.5.2.1  bouyer 	 * will be set. Check for and return an error if this condition is
   1327  1.5.2.1  bouyer 	 * found.
   1328  1.5.2.1  bouyer 	 */
   1329  1.5.2.1  bouyer 	if ((ADW_READ_WORD_REGISTER(iot,ioh, IOPW_SCSI_CTRL) & 0x3F07)==0x3F07){
   1330  1.5.2.1  bouyer 		return ADW_IERR_REVERSED_CABLE;
   1331  1.5.2.1  bouyer 	}
   1332  1.5.2.1  bouyer 
   1333  1.5.2.1  bouyer 	/*
   1334  1.5.2.1  bouyer 	 * All kind of combinations of devices attached to one of four
   1335  1.5.2.1  bouyer 	 * connectors are acceptable except HVD device attached.
   1336  1.5.2.1  bouyer 	 * For example, LVD device can be attached to SE connector while
   1337  1.5.2.1  bouyer 	 * SE device attached to LVD connector.
   1338  1.5.2.1  bouyer 	 * If LVD device attached to SE connector, it only runs up to
   1339  1.5.2.1  bouyer 	 * Ultra speed.
   1340  1.5.2.1  bouyer 	 *
   1341  1.5.2.1  bouyer 	 * If an HVD device is attached to one of LVD connectors, return
   1342  1.5.2.1  bouyer 	 * an error.
   1343  1.5.2.1  bouyer 	 * However, there is no way to detect HVD device attached to
   1344  1.5.2.1  bouyer 	 * SE connectors.
   1345  1.5.2.1  bouyer 	 */
   1346  1.5.2.1  bouyer 	if (scsi_cfg1 & ADW_HVD) {
   1347  1.5.2.1  bouyer 		return ADW_IERR_HVD_DEVICE;
   1348      1.1   dante 	}
   1349      1.1   dante 
   1350      1.1   dante 	/*
   1351  1.5.2.1  bouyer 	 * If either SE or LVD automatic termination control is enabled, then
   1352  1.5.2.1  bouyer 	 * set the termination value based on a table listed in a_condor.h.
   1353      1.1   dante 	 *
   1354  1.5.2.1  bouyer 	 * If manual termination was specified with an EEPROM setting then
   1355  1.5.2.1  bouyer 	 * 'termination' was set-up in AdwInitFromEEPROM() and is ready
   1356  1.5.2.1  bouyer 	 * to be 'ored' into SCSI_CFG1.
   1357      1.1   dante 	 */
   1358  1.5.2.1  bouyer 	if ((cfg->termination & ADW_TERM_SE) == 0) {
   1359  1.5.2.1  bouyer 		/* SE automatic termination control is enabled. */
   1360  1.5.2.1  bouyer 		switch(scsi_cfg1 & ADW_C_DET_SE) {
   1361  1.5.2.1  bouyer 			/* TERM_SE_HI: on, TERM_SE_LO: on */
   1362  1.5.2.1  bouyer 			case 0x1: case 0x2: case 0x3:
   1363  1.5.2.1  bouyer 				cfg->termination |= ADW_TERM_SE;
   1364  1.5.2.1  bouyer 				break;
   1365  1.5.2.1  bouyer 
   1366  1.5.2.1  bouyer 			/* TERM_SE_HI: on, TERM_SE_LO: off */
   1367  1.5.2.1  bouyer 			case 0x0:
   1368  1.5.2.1  bouyer 				cfg->termination |= ADW_TERM_SE_HI;
   1369  1.5.2.1  bouyer 				break;
   1370  1.5.2.1  bouyer 		}
   1371  1.5.2.1  bouyer 	}
   1372      1.2   dante 
   1373  1.5.2.1  bouyer 	if ((cfg->termination & ADW_TERM_LVD) == 0) {
   1374  1.5.2.1  bouyer 		/* LVD automatic termination control is enabled. */
   1375  1.5.2.1  bouyer 		switch(scsi_cfg1 & ADW_C_DET_LVD) {
   1376  1.5.2.1  bouyer 			/* TERM_LVD_HI: on, TERM_LVD_LO: on */
   1377  1.5.2.1  bouyer 			case 0x4: case 0x8: case 0xC:
   1378  1.5.2.1  bouyer 				cfg->termination |= ADW_TERM_LVD;
   1379  1.5.2.1  bouyer 				break;
   1380  1.5.2.1  bouyer 
   1381  1.5.2.1  bouyer 			/* TERM_LVD_HI: off, TERM_LVD_LO: off */
   1382  1.5.2.1  bouyer 			case 0x0:
   1383  1.5.2.1  bouyer 				break;
   1384  1.5.2.1  bouyer 		}
   1385  1.5.2.1  bouyer 	}
   1386      1.1   dante 
   1387      1.1   dante 	/*
   1388  1.5.2.1  bouyer 	 * Clear any set TERM_SE and TERM_LVD bits.
   1389      1.1   dante 	 */
   1390  1.5.2.1  bouyer 	scsi_cfg1 &= (~ADW_TERM_SE & ~ADW_TERM_LVD);
   1391      1.1   dante 
   1392  1.5.2.1  bouyer 	/*
   1393  1.5.2.1  bouyer 	 * Invert the TERM_SE and TERM_LVD bits and then set 'scsi_cfg1'.
   1394  1.5.2.1  bouyer 	 */
   1395  1.5.2.1  bouyer 	scsi_cfg1 |= (~cfg->termination & 0xF0);
   1396      1.1   dante 
   1397  1.5.2.1  bouyer 	/*
   1398  1.5.2.1  bouyer 	 * Clear BIG_ENDIAN, DIS_TERM_DRV, Terminator Polarity and
   1399  1.5.2.1  bouyer 	 * HVD/LVD/SE bits and set possibly modified termination control bits
   1400  1.5.2.1  bouyer 	 * in the Microcode SCSI_CFG1 Register Value.
   1401  1.5.2.1  bouyer 	 */
   1402  1.5.2.1  bouyer 	scsi_cfg1 &= (~ADW_BIG_ENDIAN & ~ADW_DIS_TERM_DRV &
   1403  1.5.2.1  bouyer 					~ADW_TERM_POL & ~ADW_HVD_LVD_SE);
   1404      1.1   dante 
   1405  1.5.2.1  bouyer 	/*
   1406  1.5.2.1  bouyer 	 * Set SCSI_CFG1 Microcode Default Value
   1407  1.5.2.1  bouyer 	 *
   1408  1.5.2.1  bouyer 	 * Set possibly modified termination control and reset DIS_TERM_DRV
   1409  1.5.2.1  bouyer 	 * bits in the Microcode SCSI_CFG1 Register Value.
   1410  1.5.2.1  bouyer 	 *
   1411  1.5.2.1  bouyer 	 * The microcode will set the SCSI_CFG1 register using this value
   1412  1.5.2.1  bouyer 	 * after it is started below.
   1413  1.5.2.1  bouyer 	 */
   1414  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);
   1415      1.2   dante 
   1416  1.5.2.1  bouyer 	/*
   1417  1.5.2.1  bouyer 	 * Set MEM_CFG Microcode Default Value
   1418  1.5.2.1  bouyer 	 *
   1419  1.5.2.1  bouyer 	 * The microcode will set the MEM_CFG register using this value
   1420  1.5.2.1  bouyer 	 * after it is started below.
   1421  1.5.2.1  bouyer 	 *
   1422  1.5.2.1  bouyer 	 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
   1423  1.5.2.1  bouyer 	 * are defined.
   1424  1.5.2.1  bouyer 	 *
   1425  1.5.2.1  bouyer 	 * ASC-38C0800 has 16KB internal memory.
   1426  1.5.2.1  bouyer 	 */
   1427  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_MEM_CFG,
   1428  1.5.2.1  bouyer 						ADW_BIOS_EN | ADW_RAM_SZ_16KB);
   1429  1.5.2.1  bouyer 
   1430  1.5.2.1  bouyer 	return 0;
   1431      1.1   dante }
   1432      1.1   dante 
   1433  1.5.2.1  bouyer 
   1434      1.1   dante int
   1435  1.5.2.1  bouyer AdwASC38C1600Cabling(iot, ioh, cfg)
   1436  1.5.2.1  bouyer 	bus_space_tag_t iot;
   1437  1.5.2.1  bouyer 	bus_space_handle_t ioh;
   1438  1.5.2.1  bouyer 	ADW_DVC_CFG *cfg;
   1439      1.1   dante {
   1440  1.5.2.1  bouyer 	u_int16_t	scsi_cfg1;
   1441      1.1   dante 
   1442      1.1   dante 
   1443      1.1   dante 	/*
   1444  1.5.2.1  bouyer 	 * Determine SCSI_CFG1 Microcode Default Value.
   1445      1.1   dante 	 *
   1446  1.5.2.1  bouyer 	 * The microcode will set the SCSI_CFG1 register using this value
   1447  1.5.2.1  bouyer 	 * after it is started below.
   1448  1.5.2.1  bouyer 	 * Each ASC-38C1600 function has only two cable detect bits.
   1449  1.5.2.1  bouyer 	 * The bus mode override bits are in IOPB_SOFT_OVER_WR.
   1450      1.1   dante 	 */
   1451      1.1   dante 
   1452  1.5.2.1  bouyer 	/* Read current SCSI_CFG1 Register value. */
   1453  1.5.2.1  bouyer 	scsi_cfg1 = ADW_READ_WORD_REGISTER(iot, ioh, IOPW_SCSI_CFG1);
   1454      1.1   dante 
   1455      1.1   dante 	/*
   1456  1.5.2.1  bouyer 	 * If the cable is reversed all of the SCSI_CTRL register signals
   1457  1.5.2.1  bouyer 	 * will be set. Check for and return an error if this condition is
   1458  1.5.2.1  bouyer 	 * found.
   1459  1.5.2.1  bouyer 	 */
   1460  1.5.2.1  bouyer 	if ((ADW_READ_WORD_REGISTER(iot,ioh, IOPW_SCSI_CTRL) & 0x3F07)==0x3F07){
   1461  1.5.2.1  bouyer 		return ADW_IERR_REVERSED_CABLE;
   1462  1.5.2.1  bouyer 	}
   1463      1.1   dante 
   1464      1.1   dante 	/*
   1465  1.5.2.1  bouyer 	 * Each ASC-38C1600 function has two connectors. Only an HVD device
   1466  1.5.2.1  bouyer 	 * can not be connected to either connector. An LVD device or SE device
   1467  1.5.2.1  bouyer 	 * may be connected to either connecor. If an SE device is connected,
   1468  1.5.2.1  bouyer 	 * then at most Ultra speed (20 Mhz) can be used on both connectors.
   1469  1.5.2.1  bouyer 	 *
   1470  1.5.2.1  bouyer 	 * If an HVD device is attached, return an error.
   1471  1.5.2.1  bouyer 	 */
   1472  1.5.2.1  bouyer 	if (scsi_cfg1 & ADW_HVD) {
   1473  1.5.2.1  bouyer 		return ADW_IERR_HVD_DEVICE;
   1474      1.1   dante 	}
   1475  1.5.2.1  bouyer 
   1476  1.5.2.1  bouyer 	/*
   1477  1.5.2.1  bouyer 	 * Each function in the ASC-38C1600 uses only the SE cable detect and
   1478  1.5.2.1  bouyer 	 * termination because there are two connectors for each function.
   1479  1.5.2.1  bouyer 	 * Each function may use either LVD or SE mode.
   1480  1.5.2.1  bouyer 	 * Corresponding the SE automatic termination control EEPROM bits are
   1481  1.5.2.1  bouyer 	 * used for each function.
   1482  1.5.2.1  bouyer 	 * Each function has its own EEPROM. If SE automatic control is enabled
   1483  1.5.2.1  bouyer 	 * for the function, then set the termination value based on a table
   1484  1.5.2.1  bouyer 	 * listed in adwlib.h.
   1485  1.5.2.1  bouyer 	 *
   1486  1.5.2.1  bouyer 	 * If manual termination is specified in the EEPROM for the function,
   1487  1.5.2.1  bouyer 	 * then 'termination' was set-up in AdwInitFromEEPROM() and is
   1488  1.5.2.1  bouyer 	 * ready to be 'ored' into SCSI_CFG1.
   1489  1.5.2.1  bouyer 	 */
   1490  1.5.2.1  bouyer 	if ((cfg->termination & ADW_TERM_SE) == 0) {
   1491  1.5.2.1  bouyer 		/* SE automatic termination control is enabled. */
   1492  1.5.2.1  bouyer 		switch(scsi_cfg1 & ADW_C_DET_SE) {
   1493  1.5.2.1  bouyer 			/* TERM_SE_HI: on, TERM_SE_LO: on */
   1494  1.5.2.1  bouyer 			case 0x1: case 0x2: case 0x3:
   1495  1.5.2.1  bouyer 				cfg->termination |= ADW_TERM_SE;
   1496  1.5.2.1  bouyer 				break;
   1497  1.5.2.1  bouyer 
   1498  1.5.2.1  bouyer 			case 0x0:
   1499  1.5.2.1  bouyer 	/* !!!!TODO!!!! */
   1500  1.5.2.1  bouyer //				if (ASC_PCI_ID2FUNC(cfg->pci_slot_info) == 0) {
   1501  1.5.2.1  bouyer 				/* Function 0 - TERM_SE_HI: off, TERM_SE_LO: off */
   1502  1.5.2.1  bouyer //				}
   1503  1.5.2.1  bouyer //				else
   1504  1.5.2.1  bouyer //				{
   1505  1.5.2.1  bouyer 				/* Function 1 - TERM_SE_HI: on, TERM_SE_LO: off */
   1506  1.5.2.1  bouyer 					cfg->termination |= ADW_TERM_SE_HI;
   1507  1.5.2.1  bouyer //				}
   1508  1.5.2.1  bouyer 				break;
   1509  1.5.2.1  bouyer 			}
   1510      1.1   dante 	}
   1511  1.5.2.1  bouyer 
   1512      1.1   dante 	/*
   1513  1.5.2.1  bouyer 	 * Clear any set TERM_SE bits.
   1514      1.1   dante 	 */
   1515  1.5.2.1  bouyer 	scsi_cfg1 &= ~ADW_TERM_SE;
   1516  1.5.2.1  bouyer 
   1517      1.1   dante 	/*
   1518  1.5.2.1  bouyer 	 * Invert the TERM_SE bits and then set 'scsi_cfg1'.
   1519      1.1   dante 	 */
   1520  1.5.2.1  bouyer 	scsi_cfg1 |= (~cfg->termination & ADW_TERM_SE);
   1521      1.1   dante 
   1522  1.5.2.1  bouyer 	/*
   1523  1.5.2.1  bouyer 	 * Clear Big Endian and Terminator Polarity bits and set possibly
   1524  1.5.2.1  bouyer 	 * modified termination control bits in the Microcode SCSI_CFG1
   1525  1.5.2.1  bouyer 	 * Register Value.
   1526  1.5.2.1  bouyer 	 */
   1527  1.5.2.1  bouyer 	scsi_cfg1 &= (~ADW_BIG_ENDIAN & ~ADW_DIS_TERM_DRV & ~ADW_TERM_POL);
   1528      1.1   dante 
   1529      1.1   dante 	/*
   1530  1.5.2.1  bouyer 	 * Set SCSI_CFG1 Microcode Default Value
   1531      1.1   dante 	 *
   1532  1.5.2.1  bouyer 	 * Set possibly modified termination control bits in the Microcode
   1533  1.5.2.1  bouyer 	 * SCSI_CFG1 Register Value.
   1534  1.5.2.1  bouyer 	 *
   1535  1.5.2.1  bouyer 	 * The microcode will set the SCSI_CFG1 register using this value
   1536  1.5.2.1  bouyer 	 * after it is started below.
   1537      1.1   dante 	 */
   1538  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_SCSI_CFG1, scsi_cfg1);
   1539      1.1   dante 
   1540  1.5.2.1  bouyer 	/*
   1541  1.5.2.1  bouyer 	 * Set MEM_CFG Microcode Default Value
   1542  1.5.2.1  bouyer 	 *
   1543  1.5.2.1  bouyer 	 * The microcode will set the MEM_CFG register using this value
   1544  1.5.2.1  bouyer 	 * after it is started below.
   1545  1.5.2.1  bouyer 	 *
   1546  1.5.2.1  bouyer 	 * MEM_CFG may be accessed as a word or byte, but only bits 0-7
   1547  1.5.2.1  bouyer 	 * are defined.
   1548  1.5.2.1  bouyer 	 *
   1549  1.5.2.1  bouyer 	 * ASC-38C1600 has 32KB internal memory.
   1550  1.5.2.1  bouyer 	 */
   1551  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_DEFAULT_MEM_CFG,
   1552  1.5.2.1  bouyer 						ADW_BIOS_EN | ADW_RAM_SZ_32KB);
   1553      1.1   dante 
   1554  1.5.2.1  bouyer 	return 0;
   1555      1.1   dante }
   1556      1.1   dante 
   1557  1.5.2.1  bouyer 
   1558      1.1   dante /*
   1559      1.1   dante  * Read EEPROM configuration into the specified buffer.
   1560      1.1   dante  *
   1561      1.1   dante  * Return a checksum based on the EEPROM configuration read.
   1562      1.1   dante  */
   1563  1.5.2.1  bouyer static u_int16_t
   1564  1.5.2.1  bouyer AdwGetEEPROMConfig(iot, ioh, cfg_buf)
   1565  1.5.2.1  bouyer 	bus_space_tag_t		iot;
   1566  1.5.2.1  bouyer 	bus_space_handle_t	ioh;
   1567  1.5.2.1  bouyer 	ADW_EEPROM		*cfg_buf;
   1568      1.1   dante {
   1569  1.5.2.1  bouyer 	u_int16_t	       wval, chksum;
   1570  1.5.2.1  bouyer 	u_int16_t	       *wbuf;
   1571  1.5.2.1  bouyer 	int		    eep_addr;
   1572  1.5.2.1  bouyer 
   1573      1.1   dante 
   1574      1.1   dante 	wbuf = (u_int16_t *) cfg_buf;
   1575      1.1   dante 	chksum = 0;
   1576      1.1   dante 
   1577      1.1   dante 	for (eep_addr = ASC_EEP_DVC_CFG_BEGIN;
   1578  1.5.2.1  bouyer 		eep_addr < ASC_EEP_DVC_CFG_END;
   1579  1.5.2.1  bouyer 		eep_addr++, wbuf++) {
   1580  1.5.2.1  bouyer 		wval = AdwReadEEPWord(iot, ioh, eep_addr);
   1581      1.1   dante 		chksum += wval;
   1582      1.1   dante 		*wbuf = wval;
   1583      1.1   dante 	}
   1584  1.5.2.1  bouyer 
   1585  1.5.2.1  bouyer 	*wbuf = AdwReadEEPWord(iot, ioh, eep_addr);
   1586      1.1   dante 	wbuf++;
   1587      1.1   dante 	for (eep_addr = ASC_EEP_DVC_CTL_BEGIN;
   1588  1.5.2.1  bouyer 			eep_addr < ASC_EEP_MAX_WORD_ADDR;
   1589  1.5.2.1  bouyer 			eep_addr++, wbuf++) {
   1590  1.5.2.1  bouyer 		*wbuf = AdwReadEEPWord(iot, ioh, eep_addr);
   1591      1.1   dante 	}
   1592  1.5.2.1  bouyer 
   1593      1.1   dante 	return chksum;
   1594      1.1   dante }
   1595      1.1   dante 
   1596  1.5.2.1  bouyer 
   1597      1.1   dante /*
   1598      1.1   dante  * Read the EEPROM from specified location
   1599      1.1   dante  */
   1600  1.5.2.1  bouyer static u_int16_t
   1601  1.5.2.1  bouyer AdwReadEEPWord(iot, ioh, eep_word_addr)
   1602  1.5.2.1  bouyer 	bus_space_tag_t		iot;
   1603  1.5.2.1  bouyer 	bus_space_handle_t	ioh;
   1604  1.5.2.1  bouyer 	int			eep_word_addr;
   1605      1.1   dante {
   1606      1.2   dante 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD,
   1607  1.5.2.1  bouyer 		ASC_EEP_CMD_READ | eep_word_addr);
   1608  1.5.2.1  bouyer 	AdwWaitEEPCmd(iot, ioh);
   1609  1.5.2.1  bouyer 
   1610      1.2   dante 	return ADW_READ_WORD_REGISTER(iot, ioh, IOPW_EE_DATA);
   1611      1.1   dante }
   1612      1.1   dante 
   1613  1.5.2.1  bouyer 
   1614      1.1   dante /*
   1615      1.1   dante  * Wait for EEPROM command to complete
   1616      1.1   dante  */
   1617      1.1   dante static void
   1618  1.5.2.1  bouyer AdwWaitEEPCmd(iot, ioh)
   1619  1.5.2.1  bouyer 	bus_space_tag_t		iot;
   1620  1.5.2.1  bouyer 	bus_space_handle_t	ioh;
   1621      1.1   dante {
   1622  1.5.2.1  bouyer 	int eep_delay_ms;
   1623      1.1   dante 
   1624  1.5.2.1  bouyer 
   1625  1.5.2.1  bouyer 	for (eep_delay_ms = 0; eep_delay_ms < ASC_EEP_DELAY_MS; eep_delay_ms++){
   1626      1.1   dante 		if (ADW_READ_WORD_REGISTER(iot, ioh, IOPW_EE_CMD) &
   1627  1.5.2.1  bouyer 				ASC_EEP_CMD_DONE) {
   1628      1.1   dante 			break;
   1629      1.1   dante 		}
   1630  1.5.2.1  bouyer 		AdwSleepMilliSecond(1);
   1631      1.1   dante 	}
   1632      1.1   dante 
   1633  1.5.2.1  bouyer 	ADW_READ_WORD_REGISTER(iot, ioh, IOPW_EE_CMD);
   1634      1.1   dante }
   1635      1.1   dante 
   1636  1.5.2.1  bouyer 
   1637      1.1   dante /*
   1638      1.1   dante  * Write the EEPROM from 'cfg_buf'.
   1639      1.1   dante  */
   1640      1.1   dante static void
   1641  1.5.2.1  bouyer AdwSetEEPROMConfig(iot, ioh, cfg_buf)
   1642  1.5.2.1  bouyer 	bus_space_tag_t		iot;
   1643  1.5.2.1  bouyer 	bus_space_handle_t	ioh;
   1644  1.5.2.1  bouyer 	ADW_EEPROM		*cfg_buf;
   1645      1.1   dante {
   1646  1.5.2.1  bouyer 	u_int16_t *wbuf;
   1647  1.5.2.1  bouyer 	u_int16_t addr, chksum;
   1648  1.5.2.1  bouyer 
   1649      1.1   dante 
   1650      1.1   dante 	wbuf = (u_int16_t *) cfg_buf;
   1651      1.1   dante 	chksum = 0;
   1652      1.1   dante 
   1653      1.1   dante 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD, ASC_EEP_CMD_WRITE_ABLE);
   1654  1.5.2.1  bouyer 	AdwWaitEEPCmd(iot, ioh);
   1655      1.1   dante 
   1656      1.1   dante 	/*
   1657  1.5.2.1  bouyer 	 * Write EEPROM from word 0 to word 20
   1658      1.1   dante 	 */
   1659      1.1   dante 	for (addr = ASC_EEP_DVC_CFG_BEGIN;
   1660      1.2   dante 	     addr < ASC_EEP_DVC_CFG_END; addr++, wbuf++) {
   1661      1.1   dante 		chksum += *wbuf;
   1662      1.1   dante 		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_DATA, *wbuf);
   1663  1.5.2.1  bouyer 		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD,
   1664  1.5.2.1  bouyer 				ASC_EEP_CMD_WRITE | addr);
   1665  1.5.2.1  bouyer 		AdwWaitEEPCmd(iot, ioh);
   1666  1.5.2.1  bouyer 		AdwSleepMilliSecond(ASC_EEP_DELAY_MS);
   1667      1.1   dante 	}
   1668      1.1   dante 
   1669      1.1   dante 	/*
   1670  1.5.2.1  bouyer 	 * Write EEPROM checksum at word 21
   1671      1.1   dante 	 */
   1672      1.1   dante 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_DATA, chksum);
   1673      1.1   dante 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD,
   1674  1.5.2.1  bouyer 			ASC_EEP_CMD_WRITE | addr);
   1675  1.5.2.1  bouyer 	AdwWaitEEPCmd(iot, ioh);
   1676  1.5.2.1  bouyer 	wbuf++;        /* skip over check_sum */
   1677      1.1   dante 
   1678      1.1   dante 	/*
   1679  1.5.2.1  bouyer 	 * Write EEPROM OEM name at words 22 to 29
   1680      1.1   dante 	 */
   1681      1.1   dante 	for (addr = ASC_EEP_DVC_CTL_BEGIN;
   1682      1.2   dante 	     addr < ASC_EEP_MAX_WORD_ADDR; addr++, wbuf++) {
   1683      1.1   dante 		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_DATA, *wbuf);
   1684  1.5.2.1  bouyer 		ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD,
   1685  1.5.2.1  bouyer 				ASC_EEP_CMD_WRITE | addr);
   1686  1.5.2.1  bouyer 		AdwWaitEEPCmd(iot, ioh);
   1687      1.1   dante 	}
   1688  1.5.2.1  bouyer 
   1689      1.1   dante 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_EE_CMD,
   1690  1.5.2.1  bouyer 			ASC_EEP_CMD_WRITE_DISABLE);
   1691  1.5.2.1  bouyer 	AdwWaitEEPCmd(iot, ioh);
   1692  1.5.2.1  bouyer 
   1693      1.1   dante 	return;
   1694      1.1   dante }
   1695      1.1   dante 
   1696  1.5.2.1  bouyer 
   1697      1.1   dante /*
   1698  1.5.2.1  bouyer  * AdwExeScsiQueue() - Send a request to the RISC microcode program.
   1699      1.1   dante  *
   1700  1.5.2.1  bouyer  *   Allocate a carrier structure, point the carrier to the ADW_SCSI_REQ_Q,
   1701  1.5.2.1  bouyer  *   add the carrier to the ICQ (Initiator Command Queue), and tickle the
   1702  1.5.2.1  bouyer  *   RISC to notify it a new command is ready to be executed.
   1703      1.1   dante  *
   1704  1.5.2.1  bouyer  * If 'done_status' is not set to QD_DO_RETRY, then 'error_retry' will be
   1705  1.5.2.1  bouyer  * set to SCSI_MAX_RETRY.
   1706  1.5.2.1  bouyer  *
   1707  1.5.2.1  bouyer  * Return:
   1708  1.5.2.1  bouyer  *      ADW_SUCCESS(1) - The request was successfully queued.
   1709  1.5.2.1  bouyer  *      ADW_BUSY(0) -    Resource unavailable; Retry again after pending
   1710  1.5.2.1  bouyer  *                       request completes.
   1711  1.5.2.1  bouyer  *      ADW_ERROR(-1) -  Invalid ADW_SCSI_REQ_Q request structure
   1712  1.5.2.1  bouyer  *                       host IC error.
   1713      1.1   dante  */
   1714  1.5.2.1  bouyer int
   1715  1.5.2.1  bouyer AdwExeScsiQueue(sc, scsiq)
   1716  1.5.2.1  bouyer ADW_SOFTC	*sc;
   1717  1.5.2.1  bouyer ADW_SCSI_REQ_Q	*scsiq;
   1718      1.1   dante {
   1719  1.5.2.1  bouyer 	bus_space_tag_t iot = sc->sc_iot;
   1720  1.5.2.1  bouyer 	bus_space_handle_t ioh = sc->sc_ioh;
   1721  1.5.2.1  bouyer 	ADW_CCB		*ccb;
   1722  1.5.2.1  bouyer 	long		req_size;
   1723  1.5.2.1  bouyer 	u_int32_t	req_paddr;
   1724  1.5.2.1  bouyer 	ADW_CARRIER	*new_carrp;
   1725      1.1   dante 
   1726  1.5.2.1  bouyer 	/*
   1727  1.5.2.1  bouyer 	 * The ADW_SCSI_REQ_Q 'target_id' field should never exceed ADW_MAX_TID.
   1728  1.5.2.1  bouyer 	 */
   1729  1.5.2.1  bouyer 	if (scsiq->target_id > ADW_MAX_TID) {
   1730  1.5.2.1  bouyer 		scsiq->host_status = QHSTA_M_INVALID_DEVICE;
   1731  1.5.2.1  bouyer 		scsiq->done_status = QD_WITH_ERROR;
   1732  1.5.2.1  bouyer 		return ADW_ERROR;
   1733  1.5.2.1  bouyer 	}
   1734      1.1   dante 
   1735      1.1   dante 	/*
   1736  1.5.2.1  bouyer 	 * Begin of CRITICAL SECTION: Must be protected within splbio/splx pair
   1737      1.1   dante 	 */
   1738  1.5.2.1  bouyer 
   1739  1.5.2.1  bouyer 	ccb = adw_ccb_phys_kv(sc, scsiq->ccb_ptr);
   1740      1.1   dante 
   1741      1.1   dante 	/*
   1742  1.5.2.1  bouyer 	 * Allocate a carrier and initialize fields.
   1743  1.5.2.1  bouyer 	 */
   1744  1.5.2.1  bouyer 	if ((new_carrp = sc->carr_freelist) == NULL) {
   1745  1.5.2.1  bouyer 		return ADW_BUSY;
   1746  1.5.2.1  bouyer 	}
   1747  1.5.2.1  bouyer 	sc->carr_freelist = ADW_CARRIER_VADDR(sc,
   1748  1.5.2.1  bouyer 			ASC_GET_CARRP(new_carrp->next_ba));
   1749  1.5.2.1  bouyer 	sc->carr_pending_cnt++;
   1750  1.5.2.1  bouyer 
   1751  1.5.2.1  bouyer 	/*
   1752  1.5.2.1  bouyer 	 * Set the carrier to be a stopper by setting 'next_ba'
   1753  1.5.2.1  bouyer 	 * to the stopper value. The current stopper will be changed
   1754  1.5.2.1  bouyer 	 * below to point to the new stopper.
   1755      1.1   dante 	 */
   1756  1.5.2.1  bouyer 	new_carrp->next_ba = ASC_CQ_STOPPER;
   1757      1.1   dante 
   1758  1.5.2.1  bouyer 	req_size = sizeof(ADW_SCSI_REQ_Q);
   1759  1.5.2.1  bouyer 	req_paddr = sc->sc_dmamap_control->dm_segs[0].ds_addr +
   1760  1.5.2.1  bouyer 		ADW_CCB_OFF(ccb) + offsetof(struct adw_ccb, scsiq);
   1761  1.5.2.1  bouyer 
   1762  1.5.2.1  bouyer 	/* Save physical address of ADW_SCSI_REQ_Q and Carrier. */
   1763  1.5.2.1  bouyer 	scsiq->scsiq_rptr = req_paddr;
   1764      1.1   dante 
   1765      1.1   dante 	/*
   1766  1.5.2.1  bouyer 	 * Every ADV_CARR_T.carr_ba is byte swapped to little-endian
   1767  1.5.2.1  bouyer 	 * order during initialization.
   1768      1.1   dante 	 */
   1769  1.5.2.1  bouyer 	scsiq->carr_ba = sc->icq_sp->carr_ba;
   1770  1.5.2.1  bouyer 	scsiq->carr_va = sc->icq_sp->carr_ba;
   1771  1.5.2.1  bouyer 
   1772  1.5.2.1  bouyer 	/*
   1773  1.5.2.1  bouyer 	 * Use the current stopper to send the ADW_SCSI_REQ_Q command to
   1774  1.5.2.1  bouyer 	 * the microcode. The newly allocated stopper will become the new
   1775  1.5.2.1  bouyer 	 * stopper.
   1776  1.5.2.1  bouyer 	 */
   1777  1.5.2.1  bouyer 	sc->icq_sp->areq_ba = req_paddr;
   1778  1.5.2.1  bouyer 
   1779  1.5.2.1  bouyer 	/*
   1780  1.5.2.1  bouyer 	 * Set the 'next_ba' pointer for the old stopper to be the
   1781  1.5.2.1  bouyer 	 * physical address of the new stopper. The RISC can only
   1782  1.5.2.1  bouyer 	 * follow physical addresses.
   1783  1.5.2.1  bouyer 	 */
   1784  1.5.2.1  bouyer 	sc->icq_sp->next_ba = new_carrp->carr_ba;
   1785  1.5.2.1  bouyer 
   1786  1.5.2.1  bouyer #if ADW_DEBUG
   1787  1.5.2.1  bouyer 	printf("icq 0x%x, 0x%x, 0x%x, 0x%x\n",
   1788  1.5.2.1  bouyer 			sc->icq_sp->carr_id,
   1789  1.5.2.1  bouyer 			sc->icq_sp->carr_ba,
   1790  1.5.2.1  bouyer 			sc->icq_sp->areq_ba,
   1791  1.5.2.1  bouyer 			sc->icq_sp->next_ba);
   1792  1.5.2.1  bouyer #endif
   1793  1.5.2.1  bouyer 	/*
   1794  1.5.2.1  bouyer 	 * Set the host adapter stopper pointer to point to the new carrier.
   1795  1.5.2.1  bouyer 	 */
   1796  1.5.2.1  bouyer 	sc->icq_sp = new_carrp;
   1797  1.5.2.1  bouyer 
   1798  1.5.2.1  bouyer 	if (sc->chip_type == ADW_CHIP_ASC3550 ||
   1799  1.5.2.1  bouyer 	    sc->chip_type == ADW_CHIP_ASC38C0800) {
   1800  1.5.2.1  bouyer 		/*
   1801  1.5.2.1  bouyer 		 * Tickle the RISC to tell it to read its Command Queue Head
   1802  1.5.2.1  bouyer 		 * pointer.
   1803  1.5.2.1  bouyer 		 */
   1804  1.5.2.1  bouyer 		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_TICKLE, ADW_TICKLE_A);
   1805  1.5.2.1  bouyer 		if (sc->chip_type == ADW_CHIP_ASC3550) {
   1806  1.5.2.1  bouyer 			/*
   1807  1.5.2.1  bouyer 			 * Clear the tickle value. In the ASC-3550 the RISC flag
   1808  1.5.2.1  bouyer 			 * command 'clr_tickle_a' does not work unless the host
   1809  1.5.2.1  bouyer 			 * value is cleared.
   1810  1.5.2.1  bouyer 			 */
   1811  1.5.2.1  bouyer 			ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_TICKLE,
   1812  1.5.2.1  bouyer 					ADW_TICKLE_NOP);
   1813  1.5.2.1  bouyer 		}
   1814  1.5.2.1  bouyer 	} else if (sc->chip_type == ADW_CHIP_ASC38C1600) {
   1815  1.5.2.1  bouyer 		/*
   1816  1.5.2.1  bouyer 		 * Notify the RISC a carrier is ready by writing the physical
   1817  1.5.2.1  bouyer 		 * address of the new carrier stopper to the COMMA register.
   1818  1.5.2.1  bouyer 		 */
   1819  1.5.2.1  bouyer 		ADW_WRITE_DWORD_REGISTER(iot, ioh, IOPDW_COMMA,
   1820  1.5.2.1  bouyer 				new_carrp->carr_ba);
   1821  1.5.2.1  bouyer 	}
   1822  1.5.2.1  bouyer 
   1823  1.5.2.1  bouyer 	/*
   1824  1.5.2.1  bouyer 	 * End of CRITICAL SECTION: Must be protected within splbio/splx pair
   1825  1.5.2.1  bouyer 	 */
   1826  1.5.2.1  bouyer 
   1827  1.5.2.1  bouyer 	return ADW_SUCCESS;
   1828      1.1   dante }
   1829      1.1   dante 
   1830  1.5.2.1  bouyer 
   1831  1.5.2.1  bouyer void
   1832  1.5.2.1  bouyer AdwResetChip(iot, ioh)
   1833  1.5.2.1  bouyer 	bus_space_tag_t iot;
   1834  1.5.2.1  bouyer 	bus_space_handle_t ioh;
   1835      1.1   dante {
   1836  1.5.2.1  bouyer 
   1837  1.5.2.1  bouyer 	/*
   1838  1.5.2.1  bouyer 	 * Reset Chip.
   1839  1.5.2.1  bouyer 	 */
   1840  1.5.2.1  bouyer 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_CTRL_REG,
   1841  1.5.2.1  bouyer 			ADW_CTRL_REG_CMD_RESET);
   1842  1.5.2.1  bouyer 	AdwSleepMilliSecond(100);
   1843  1.5.2.1  bouyer 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_CTRL_REG,
   1844  1.5.2.1  bouyer 			ADW_CTRL_REG_CMD_WR_IO_REG);
   1845      1.1   dante }
   1846      1.1   dante 
   1847  1.5.2.1  bouyer 
   1848      1.1   dante /*
   1849      1.1   dante  * Reset SCSI Bus and purge all outstanding requests.
   1850      1.1   dante  *
   1851      1.1   dante  * Return Value:
   1852  1.5.2.1  bouyer  *      ADW_TRUE(1) -   All requests are purged and SCSI Bus is reset.
   1853  1.5.2.1  bouyer  *      ADW_FALSE(0) -  Microcode command failed.
   1854  1.5.2.1  bouyer  *      ADW_ERROR(-1) - Microcode command timed-out. Microcode or IC
   1855  1.5.2.1  bouyer  *                      may be hung which requires driver recovery.
   1856      1.1   dante  */
   1857      1.1   dante int
   1858  1.5.2.1  bouyer AdwResetCCB(sc)
   1859  1.5.2.1  bouyer ADW_SOFTC	*sc;
   1860      1.1   dante {
   1861  1.5.2.1  bouyer 	int	    status;
   1862  1.5.2.1  bouyer 
   1863  1.5.2.1  bouyer 	/*
   1864  1.5.2.1  bouyer 	 * Send the SCSI Bus Reset idle start idle command which asserts
   1865  1.5.2.1  bouyer 	 * the SCSI Bus Reset signal.
   1866  1.5.2.1  bouyer 	 */
   1867  1.5.2.1  bouyer 	status = AdwSendIdleCmd(sc, (u_int16_t) IDLE_CMD_SCSI_RESET_START, 0L);
   1868  1.5.2.1  bouyer 	if (status != ADW_TRUE) {
   1869  1.5.2.1  bouyer 		return status;
   1870  1.5.2.1  bouyer 	}
   1871  1.5.2.1  bouyer 
   1872  1.5.2.1  bouyer 	/*
   1873  1.5.2.1  bouyer 	 * Delay for the specified SCSI Bus Reset hold time.
   1874  1.5.2.1  bouyer 	 *
   1875  1.5.2.1  bouyer 	 * The hold time delay is done on the host because the RISC has no
   1876  1.5.2.1  bouyer 	 * microsecond accurate timer.
   1877  1.5.2.1  bouyer 	 */
   1878  1.5.2.1  bouyer 	AdwDelayMicroSecond((u_int16_t) ASC_SCSI_RESET_HOLD_TIME_US);
   1879      1.1   dante 
   1880  1.5.2.1  bouyer 	/*
   1881  1.5.2.1  bouyer 	 * Send the SCSI Bus Reset end idle command which de-asserts
   1882  1.5.2.1  bouyer 	 * the SCSI Bus Reset signal and purges any pending requests.
   1883  1.5.2.1  bouyer 	 */
   1884  1.5.2.1  bouyer 	status = AdwSendIdleCmd(sc, (u_int16_t) IDLE_CMD_SCSI_RESET_END, 0L);
   1885  1.5.2.1  bouyer 	if (status != ADW_TRUE) {
   1886  1.5.2.1  bouyer 		return status;
   1887  1.5.2.1  bouyer 	}
   1888      1.1   dante 
   1889  1.5.2.1  bouyer 	AdwSleepMilliSecond((u_int32_t) sc->scsi_reset_wait * 1000);
   1890      1.1   dante 
   1891      1.1   dante 	return status;
   1892      1.1   dante }
   1893      1.1   dante 
   1894  1.5.2.1  bouyer 
   1895      1.1   dante /*
   1896  1.5.2.1  bouyer  * Reset chip and SCSI Bus.
   1897  1.5.2.1  bouyer  *
   1898  1.5.2.1  bouyer  * Return Value:
   1899  1.5.2.1  bouyer  *      ADW_TRUE(1) -   Chip re-initialization and SCSI Bus Reset successful.
   1900  1.5.2.1  bouyer  *      ADW_FALSE(0) -  Chip re-initialization and SCSI Bus Reset failure.
   1901      1.1   dante  */
   1902  1.5.2.1  bouyer int
   1903  1.5.2.1  bouyer AdwResetSCSIBus(sc)
   1904  1.5.2.1  bouyer ADW_SOFTC	*sc;
   1905      1.1   dante {
   1906      1.2   dante 	bus_space_tag_t iot = sc->sc_iot;
   1907      1.2   dante 	bus_space_handle_t ioh = sc->sc_ioh;
   1908  1.5.2.1  bouyer 	int		status;
   1909  1.5.2.1  bouyer 	u_int16_t	wdtr_able, sdtr_able, ppr_able, tagqng_able;
   1910  1.5.2.1  bouyer 	u_int8_t	tid, max_cmd[ADW_MAX_TID + 1];
   1911  1.5.2.1  bouyer 	u_int16_t	bios_sig;
   1912  1.5.2.1  bouyer 
   1913  1.5.2.1  bouyer 
   1914  1.5.2.1  bouyer 	/*
   1915  1.5.2.1  bouyer 	 * Save current per TID negotiated values.
   1916  1.5.2.1  bouyer 	 */
   1917  1.5.2.1  bouyer 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE, wdtr_able);
   1918  1.5.2.1  bouyer 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, sdtr_able);
   1919  1.5.2.1  bouyer 	if (sc->chip_type == ADW_CHIP_ASC38C1600) {
   1920  1.5.2.1  bouyer 		ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_PPR_ABLE, ppr_able);
   1921  1.5.2.1  bouyer 	}
   1922  1.5.2.1  bouyer 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE, tagqng_able);
   1923  1.5.2.1  bouyer 	for (tid = 0; tid <= ADW_MAX_TID; tid++) {
   1924  1.5.2.1  bouyer 		ADW_READ_BYTE_LRAM(iot, ioh, ADW_MC_NUMBER_OF_MAX_CMD + tid,
   1925  1.5.2.1  bouyer 			max_cmd[tid]);
   1926  1.5.2.1  bouyer 	}
   1927  1.5.2.1  bouyer 
   1928  1.5.2.1  bouyer 	/*
   1929  1.5.2.1  bouyer 	 * Force the AdwInitAscDriver() function to perform a SCSI Bus Reset
   1930  1.5.2.1  bouyer 	 * by clearing the BIOS signature word.
   1931  1.5.2.1  bouyer 	 * The initialization functions assumes a SCSI Bus Reset is not
   1932  1.5.2.1  bouyer 	 * needed if the BIOS signature word is present.
   1933  1.5.2.1  bouyer 	 */
   1934  1.5.2.1  bouyer 	ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_BIOS_SIGNATURE, bios_sig);
   1935  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_BIOS_SIGNATURE, 0);
   1936  1.5.2.1  bouyer 
   1937  1.5.2.1  bouyer 	/*
   1938  1.5.2.1  bouyer 	 * Stop chip and reset it.
   1939  1.5.2.1  bouyer 	 */
   1940  1.5.2.1  bouyer 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_RISC_CSR, ADW_RISC_CSR_STOP);
   1941  1.5.2.1  bouyer 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_CTRL_REG,
   1942  1.5.2.1  bouyer 			ADW_CTRL_REG_CMD_RESET);
   1943  1.5.2.1  bouyer 	AdwSleepMilliSecond(100);
   1944  1.5.2.1  bouyer 	ADW_WRITE_WORD_REGISTER(iot, ioh, IOPW_CTRL_REG,
   1945  1.5.2.1  bouyer 			ADW_CTRL_REG_CMD_WR_IO_REG);
   1946      1.1   dante 
   1947  1.5.2.1  bouyer 	/*
   1948  1.5.2.1  bouyer 	 * Reset Adv Library error code, if any, and try
   1949  1.5.2.1  bouyer 	 * re-initializing the chip.
   1950  1.5.2.1  bouyer 	 * Then translate initialization return value to status value.
   1951  1.5.2.1  bouyer 	 */
   1952  1.5.2.1  bouyer 	status = (AdwInitDriver(sc) == 0)? ADW_TRUE : ADW_FALSE;
   1953      1.1   dante 
   1954  1.5.2.1  bouyer 	/*
   1955  1.5.2.1  bouyer 	 * Restore the BIOS signature word.
   1956  1.5.2.1  bouyer 	 */
   1957  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_BIOS_SIGNATURE, bios_sig);
   1958      1.1   dante 
   1959      1.1   dante 	/*
   1960  1.5.2.1  bouyer 	 * Restore per TID negotiated values.
   1961      1.1   dante 	 */
   1962  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE, wdtr_able);
   1963  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE, sdtr_able);
   1964  1.5.2.1  bouyer 	if (sc->chip_type == ADW_CHIP_ASC38C1600) {
   1965  1.5.2.1  bouyer 		ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_PPR_ABLE, ppr_able);
   1966  1.5.2.1  bouyer 	}
   1967  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE, tagqng_able);
   1968  1.5.2.1  bouyer 	for (tid = 0; tid <= ADW_MAX_TID; tid++) {
   1969  1.5.2.1  bouyer 		ADW_WRITE_BYTE_LRAM(iot, ioh, ADW_MC_NUMBER_OF_MAX_CMD + tid,
   1970  1.5.2.1  bouyer 			max_cmd[tid]);
   1971  1.5.2.1  bouyer 	}
   1972      1.1   dante 
   1973  1.5.2.1  bouyer 	return status;
   1974      1.1   dante }
   1975      1.1   dante 
   1976      1.1   dante 
   1977      1.1   dante /*
   1978      1.1   dante  * Adv Library Interrupt Service Routine
   1979      1.1   dante  *
   1980      1.1   dante  *  This function is called by a driver's interrupt service routine.
   1981      1.1   dante  *  The function disables and re-enables interrupts.
   1982      1.1   dante  *
   1983  1.5.2.1  bouyer  *  When a microcode idle command is completed, the ADV_DVC_VAR
   1984      1.1   dante  *  'idle_cmd_done' field is set to ADW_TRUE.
   1985      1.1   dante  *
   1986  1.5.2.1  bouyer  *  Note: AdwISR() can be called when interrupts are disabled or even
   1987      1.1   dante  *  when there is no hardware interrupt condition present. It will
   1988      1.1   dante  *  always check for completed idle commands and microcode requests.
   1989      1.1   dante  *  This is an important feature that shouldn't be changed because it
   1990      1.1   dante  *  allows commands to be completed from polling mode loops.
   1991      1.1   dante  *
   1992      1.1   dante  * Return:
   1993      1.1   dante  *   ADW_TRUE(1) - interrupt was pending
   1994      1.1   dante  *   ADW_FALSE(0) - no interrupt was pending
   1995      1.1   dante  */
   1996      1.1   dante int
   1997  1.5.2.1  bouyer AdwISR(sc)
   1998  1.5.2.1  bouyer ADW_SOFTC	*sc;
   1999      1.1   dante {
   2000      1.2   dante 	bus_space_tag_t iot = sc->sc_iot;
   2001      1.2   dante 	bus_space_handle_t ioh = sc->sc_ioh;
   2002  1.5.2.1  bouyer 	u_int8_t	int_stat;
   2003  1.5.2.1  bouyer 	u_int16_t	target_bit;
   2004  1.5.2.1  bouyer 	ADW_CARRIER	*free_carrp/*, *ccb_carr*/;
   2005  1.5.2.1  bouyer 	u_int32_t	irq_next_pa;
   2006  1.5.2.1  bouyer 	ADW_SCSI_REQ_Q	*scsiq;
   2007  1.5.2.1  bouyer 	ADW_CCB		*ccb;
   2008  1.5.2.1  bouyer 	int		s;
   2009      1.1   dante 
   2010      1.1   dante 
   2011  1.5.2.1  bouyer 	s = splbio();
   2012      1.1   dante 
   2013      1.1   dante 	/* Reading the register clears the interrupt. */
   2014      1.1   dante 	int_stat = ADW_READ_BYTE_REGISTER(iot, ioh, IOPB_INTR_STATUS_REG);
   2015      1.1   dante 
   2016  1.5.2.1  bouyer 	if ((int_stat & (ADW_INTR_STATUS_INTRA | ADW_INTR_STATUS_INTRB |
   2017  1.5.2.1  bouyer 	     ADW_INTR_STATUS_INTRC)) == 0) {
   2018  1.5.2.1  bouyer 		splx(s);
   2019  1.5.2.1  bouyer 		return ADW_FALSE;
   2020      1.1   dante 	}
   2021  1.5.2.1  bouyer 
   2022      1.1   dante 	/*
   2023  1.5.2.1  bouyer 	 * Notify the driver of an asynchronous microcode condition by
   2024  1.5.2.1  bouyer 	 * calling the ADV_DVC_VAR.async_callback function. The function
   2025  1.5.2.1  bouyer 	 * is passed the microcode ADW_MC_INTRB_CODE byte value.
   2026      1.1   dante 	 */
   2027  1.5.2.1  bouyer 	if (int_stat & ADW_INTR_STATUS_INTRB) {
   2028  1.5.2.1  bouyer 		u_int8_t intrb_code;
   2029      1.1   dante 
   2030  1.5.2.1  bouyer 		ADW_READ_BYTE_LRAM(iot, ioh, ADW_MC_INTRB_CODE, intrb_code);
   2031      1.1   dante 
   2032  1.5.2.1  bouyer 		if (sc->chip_type == ADW_CHIP_ASC3550 ||
   2033  1.5.2.1  bouyer 	    	    sc->chip_type == ADW_CHIP_ASC38C0800) {
   2034  1.5.2.1  bouyer 			if (intrb_code == ADV_ASYNC_CARRIER_READY_FAILURE &&
   2035  1.5.2.1  bouyer 				sc->carr_pending_cnt != 0) {
   2036  1.5.2.1  bouyer 				ADW_WRITE_BYTE_REGISTER(iot, ioh,
   2037  1.5.2.1  bouyer 					IOPB_TICKLE, ADW_TICKLE_A);
   2038  1.5.2.1  bouyer 				if (sc->chip_type == ADW_CHIP_ASC3550) {
   2039  1.5.2.1  bouyer 					ADW_WRITE_BYTE_REGISTER(iot, ioh,
   2040  1.5.2.1  bouyer 						IOPB_TICKLE, ADW_TICKLE_NOP);
   2041  1.5.2.1  bouyer 				}
   2042  1.5.2.1  bouyer 			}
   2043  1.5.2.1  bouyer 		}
   2044      1.1   dante 
   2045  1.5.2.1  bouyer 		if (sc->async_callback != 0) {
   2046  1.5.2.1  bouyer 		    (*(ADW_ASYNC_CALLBACK)sc->async_callback)(sc, intrb_code);
   2047  1.5.2.1  bouyer 		}
   2048  1.5.2.1  bouyer 	}
   2049      1.1   dante 
   2050  1.5.2.1  bouyer 	/*
   2051  1.5.2.1  bouyer 	 * Check if the IRQ stopper carrier contains a completed request.
   2052  1.5.2.1  bouyer 	 */
   2053  1.5.2.1  bouyer 	while (((irq_next_pa = sc->irq_sp->next_ba) & ASC_RQ_DONE) != 0)
   2054  1.5.2.1  bouyer 	{
   2055  1.5.2.1  bouyer #if ADW_DEBUG
   2056  1.5.2.1  bouyer 		printf("irq 0x%x, 0x%x, 0x%x, 0x%x\n",
   2057  1.5.2.1  bouyer 				sc->irq_sp->carr_id,
   2058  1.5.2.1  bouyer 				sc->irq_sp->carr_ba,
   2059  1.5.2.1  bouyer 				sc->irq_sp->areq_ba,
   2060  1.5.2.1  bouyer 				sc->irq_sp->next_ba);
   2061  1.5.2.1  bouyer #endif
   2062      1.1   dante 		/*
   2063  1.5.2.1  bouyer 		 * Get a pointer to the newly completed ADW_SCSI_REQ_Q
   2064  1.5.2.1  bouyer 		 * structure.
   2065  1.5.2.1  bouyer 		 * The RISC will have set 'areq_ba' to a virtual address.
   2066      1.1   dante 		 *
   2067  1.5.2.1  bouyer 		 * The firmware will have copied the ASC_SCSI_REQ_Q.ccb_ptr
   2068  1.5.2.1  bouyer 		 * field to the carrier ADV_CARR_T.areq_ba field.
   2069  1.5.2.1  bouyer 		 * The conversion below complements the conversion of
   2070  1.5.2.1  bouyer 		 * ASC_SCSI_REQ_Q.scsiq_ptr' in AdwExeScsiQueue().
   2071      1.1   dante 		 */
   2072  1.5.2.1  bouyer 		ccb = adw_ccb_phys_kv(sc, sc->irq_sp->areq_ba);
   2073  1.5.2.1  bouyer 		scsiq = &ccb->scsiq;
   2074  1.5.2.1  bouyer 		scsiq->ccb_ptr = sc->irq_sp->areq_ba;
   2075      1.1   dante 
   2076  1.5.2.1  bouyer 		/*
   2077  1.5.2.1  bouyer 		 * Request finished with good status and the queue was not
   2078  1.5.2.1  bouyer 		 * DMAed to host memory by the firmware. Set all status fields
   2079  1.5.2.1  bouyer 		 * to indicate good status.
   2080  1.5.2.1  bouyer 		 */
   2081  1.5.2.1  bouyer 		if ((irq_next_pa & ASC_RQ_GOOD) != 0) {
   2082  1.5.2.1  bouyer 			scsiq->done_status = QD_NO_ERROR;
   2083  1.5.2.1  bouyer 			scsiq->host_status = scsiq->scsi_status = 0;
   2084  1.5.2.1  bouyer 			scsiq->data_cnt = 0L;
   2085      1.1   dante 		}
   2086      1.1   dante 
   2087  1.5.2.1  bouyer 		/*
   2088  1.5.2.1  bouyer 		 * Advance the stopper pointer to the next carrier
   2089  1.5.2.1  bouyer 		 * ignoring the lower four bits. Free the previous
   2090  1.5.2.1  bouyer 		 * stopper carrier.
   2091  1.5.2.1  bouyer 		 */
   2092  1.5.2.1  bouyer 		free_carrp = sc->irq_sp;
   2093  1.5.2.1  bouyer 		sc->irq_sp = ADW_CARRIER_VADDR(sc, ASC_GET_CARRP(irq_next_pa));
   2094  1.5.2.1  bouyer 
   2095  1.5.2.1  bouyer 		free_carrp->next_ba = (sc->carr_freelist == NULL)? NULL
   2096  1.5.2.1  bouyer 					: sc->carr_freelist->carr_ba;
   2097  1.5.2.1  bouyer 		sc->carr_freelist = free_carrp;
   2098  1.5.2.1  bouyer 		sc->carr_pending_cnt--;
   2099  1.5.2.1  bouyer 
   2100  1.5.2.1  bouyer 
   2101      1.1   dante 		target_bit = ADW_TID_TO_TIDMASK(scsiq->target_id);
   2102      1.1   dante 
   2103      1.1   dante 		/*
   2104      1.1   dante 		 * Clear request microcode control flag.
   2105      1.1   dante 		 */
   2106      1.1   dante 		scsiq->cntl = 0;
   2107      1.1   dante 
   2108      1.1   dante 		/*
   2109      1.1   dante 		 * Check Condition handling
   2110      1.1   dante 		 */
   2111      1.1   dante 		/*
   2112      1.1   dante 		 * If the command that completed was a SCSI INQUIRY and
   2113      1.1   dante 		 * LUN 0 was sent the command, then process the INQUIRY
   2114      1.1   dante 		 * command information for the device.
   2115      1.1   dante 		 */
   2116  1.5.2.1  bouyer 		if (scsiq->done_status == QD_NO_ERROR &&
   2117  1.5.2.1  bouyer 		    scsiq->cdb[0] == INQUIRY &&
   2118  1.5.2.1  bouyer 		    scsiq->target_lun == 0) {
   2119  1.5.2.1  bouyer 			AdwInquiryHandling(sc, scsiq);
   2120  1.5.2.1  bouyer 		}
   2121      1.1   dante 
   2122      1.1   dante 		/*
   2123      1.1   dante 		 * Notify the driver of the completed request by passing
   2124      1.1   dante 		 * the ADW_SCSI_REQ_Q pointer to its callback function.
   2125      1.1   dante 		 */
   2126  1.5.2.1  bouyer 		(*(ADW_ISR_CALLBACK)sc->isr_callback)(sc, scsiq);
   2127      1.1   dante 		/*
   2128      1.1   dante 		 * Note: After the driver callback function is called, 'scsiq'
   2129      1.1   dante 		 * can no longer be referenced.
   2130      1.1   dante 		 *
   2131      1.1   dante 		 * Fall through and continue processing other completed
   2132      1.1   dante 		 * requests...
   2133      1.1   dante 		 */
   2134      1.1   dante 	}
   2135  1.5.2.1  bouyer 
   2136  1.5.2.1  bouyer 	splx(s);
   2137  1.5.2.1  bouyer 
   2138  1.5.2.1  bouyer 	return ADW_TRUE;
   2139      1.1   dante }
   2140      1.1   dante 
   2141  1.5.2.1  bouyer 
   2142      1.1   dante /*
   2143      1.1   dante  * Send an idle command to the chip and wait for completion.
   2144      1.1   dante  *
   2145  1.5.2.1  bouyer  * Command completion is polled for once per microsecond.
   2146  1.5.2.1  bouyer  *
   2147  1.5.2.1  bouyer  * The function can be called from anywhere including an interrupt handler.
   2148  1.5.2.1  bouyer  * But the function is not re-entrant, so it uses the splbio/splx()
   2149  1.5.2.1  bouyer  * functions to prevent reentrancy.
   2150      1.1   dante  *
   2151      1.1   dante  * Return Values:
   2152      1.1   dante  *   ADW_TRUE - command completed successfully
   2153      1.1   dante  *   ADW_FALSE - command failed
   2154  1.5.2.1  bouyer  *   ADW_ERROR - command timed out
   2155      1.1   dante  */
   2156      1.1   dante int
   2157  1.5.2.1  bouyer AdwSendIdleCmd(sc, idle_cmd, idle_cmd_parameter)
   2158  1.5.2.1  bouyer ADW_SOFTC      *sc;
   2159  1.5.2.1  bouyer u_int16_t       idle_cmd;
   2160  1.5.2.1  bouyer u_int32_t       idle_cmd_parameter;
   2161      1.1   dante {
   2162      1.2   dante 	bus_space_tag_t iot = sc->sc_iot;
   2163      1.2   dante 	bus_space_handle_t ioh = sc->sc_ioh;
   2164  1.5.2.1  bouyer 	u_int16_t	result;
   2165  1.5.2.1  bouyer 	u_int32_t	i, j, s;
   2166  1.5.2.1  bouyer 
   2167  1.5.2.1  bouyer 	s = splbio();
   2168      1.1   dante 
   2169  1.5.2.1  bouyer 	/*
   2170  1.5.2.1  bouyer 	 * Clear the idle command status which is set by the microcode
   2171  1.5.2.1  bouyer 	 * to a non-zero value to indicate when the command is completed.
   2172  1.5.2.1  bouyer 	 */
   2173  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_IDLE_CMD_STATUS, (u_int16_t) 0);
   2174      1.1   dante 
   2175      1.1   dante 	/*
   2176      1.1   dante 	 * Write the idle command value after the idle command parameter
   2177      1.1   dante 	 * has been written to avoid a race condition. If the order is not
   2178      1.1   dante 	 * followed, the microcode may process the idle command before the
   2179      1.1   dante 	 * parameters have been written to LRAM.
   2180      1.1   dante 	 */
   2181  1.5.2.1  bouyer 	ADW_WRITE_DWORD_LRAM(iot, ioh, ADW_MC_IDLE_CMD_PARAMETER,
   2182  1.5.2.1  bouyer 			idle_cmd_parameter);
   2183  1.5.2.1  bouyer 	ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_IDLE_CMD, idle_cmd);
   2184      1.1   dante 
   2185      1.1   dante 	/*
   2186  1.5.2.1  bouyer 	 * Tickle the RISC to tell it to process the idle command.
   2187      1.1   dante 	 */
   2188  1.5.2.1  bouyer 	ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_TICKLE, ADW_TICKLE_B);
   2189  1.5.2.1  bouyer 	if (sc->chip_type == ADW_CHIP_ASC3550) {
   2190      1.1   dante 		/*
   2191  1.5.2.1  bouyer 		 * Clear the tickle value. In the ASC-3550 the RISC flag
   2192  1.5.2.1  bouyer 		 * command 'clr_tickle_b' does not work unless the host
   2193  1.5.2.1  bouyer 		 * value is cleared.
   2194      1.1   dante 		 */
   2195  1.5.2.1  bouyer 		ADW_WRITE_BYTE_REGISTER(iot, ioh, IOPB_TICKLE, ADW_TICKLE_NOP);
   2196      1.1   dante 	}
   2197      1.1   dante 
   2198  1.5.2.1  bouyer 	/* Wait for up to 100 millisecond for the idle command to timeout. */
   2199  1.5.2.1  bouyer 	for (i = 0; i < SCSI_WAIT_100_MSEC; i++) {
   2200  1.5.2.1  bouyer 		/* Poll once each microsecond for command completion. */
   2201  1.5.2.1  bouyer 		for (j = 0; j < SCSI_US_PER_MSEC; j++) {
   2202  1.5.2.1  bouyer 			ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_IDLE_CMD_STATUS,
   2203  1.5.2.1  bouyer 									result);
   2204  1.5.2.1  bouyer 			if (result != 0) {
   2205  1.5.2.1  bouyer 				splx(s);
   2206  1.5.2.1  bouyer 				return result;
   2207  1.5.2.1  bouyer 			}
   2208  1.5.2.1  bouyer 			AdwDelayMicroSecond(1);
   2209  1.5.2.1  bouyer 		}
   2210      1.1   dante 	}
   2211      1.1   dante 
   2212  1.5.2.1  bouyer 	splx(s);
   2213  1.5.2.1  bouyer 	return ADW_ERROR;
   2214      1.1   dante }
   2215      1.1   dante 
   2216  1.5.2.1  bouyer 
   2217      1.1   dante /*
   2218      1.1   dante  * Inquiry Information Byte 7 Handling
   2219      1.1   dante  *
   2220      1.1   dante  * Handle SCSI Inquiry Command information for a device by setting
   2221      1.2   dante  * microcode operating variables that affect WDTR, SDTR, and Tag
   2222      1.1   dante  * Queuing.
   2223      1.1   dante  */
   2224      1.1   dante static void
   2225  1.5.2.1  bouyer AdwInquiryHandling(sc, scsiq)
   2226  1.5.2.1  bouyer ADW_SOFTC	*sc;
   2227  1.5.2.1  bouyer ADW_SCSI_REQ_Q *scsiq;
   2228      1.1   dante {
   2229  1.5.2.1  bouyer #ifndef FAILSAFE
   2230      1.2   dante 	bus_space_tag_t iot = sc->sc_iot;
   2231      1.2   dante 	bus_space_handle_t ioh = sc->sc_ioh;
   2232  1.5.2.1  bouyer 	u_int8_t		tid;
   2233  1.5.2.1  bouyer 	struct scsipi_inquiry_data *inq;
   2234  1.5.2.1  bouyer 	u_int16_t		tidmask;
   2235  1.5.2.1  bouyer 	u_int16_t		cfg_word;
   2236  1.5.2.1  bouyer 
   2237      1.1   dante 
   2238      1.1   dante 	/*
   2239  1.5.2.1  bouyer 	 * AdwInquiryHandling() requires up to INQUIRY information Byte 7
   2240      1.1   dante 	 * to be available.
   2241      1.1   dante 	 *
   2242      1.1   dante 	 * If less than 8 bytes of INQUIRY information were requested or less
   2243      1.1   dante 	 * than 8 bytes were transferred, then return. cdb[4] is the request
   2244      1.1   dante 	 * length and the ADW_SCSI_REQ_Q 'data_cnt' field is set by the
   2245      1.1   dante 	 * microcode to the transfer residual count.
   2246      1.1   dante 	 */
   2247  1.5.2.1  bouyer 
   2248      1.2   dante 	if (scsiq->cdb[4] < 8 || (scsiq->cdb[4] - scsiq->data_cnt) < 8) {
   2249      1.1   dante 		return;
   2250      1.1   dante 	}
   2251  1.5.2.1  bouyer 
   2252      1.1   dante 	tid = scsiq->target_id;
   2253  1.5.2.1  bouyer 
   2254  1.5.2.1  bouyer 	inq = (struct scsipi_inquiry_data *) scsiq->vdata_addr;
   2255      1.1   dante 
   2256      1.1   dante 	/*
   2257      1.1   dante 	 * WDTR, SDTR, and Tag Queuing cannot be enabled for old devices.
   2258      1.1   dante 	 */
   2259  1.5.2.1  bouyer 	if (((inq->response_format & SID_RespDataFmt) < 2) /*SCSI-1 | CCS*/ &&
   2260  1.5.2.1  bouyer 	    ((inq->version & SID_ANSII) < 2)) {
   2261      1.1   dante 		return;
   2262      1.2   dante 	} else {
   2263      1.1   dante 		/*
   2264      1.1   dante 		 * INQUIRY Byte 7 Handling
   2265      1.1   dante 		 *
   2266      1.1   dante 		 * Use a device's INQUIRY byte 7 to determine whether it
   2267      1.1   dante 		 * supports WDTR, SDTR, and Tag Queuing. If the feature
   2268      1.1   dante 		 * is enabled in the EEPROM and the device supports the
   2269      1.1   dante 		 * feature, then enable it in the microcode.
   2270      1.1   dante 		 */
   2271      1.1   dante 
   2272      1.1   dante 		tidmask = ADW_TID_TO_TIDMASK(tid);
   2273  1.5.2.1  bouyer 
   2274      1.1   dante 		/*
   2275      1.1   dante 		 * Wide Transfers
   2276      1.1   dante 		 *
   2277      1.1   dante 		 * If the EEPROM enabled WDTR for the device and the device
   2278      1.1   dante 		 * supports wide bus (16 bit) transfers, then turn on the
   2279      1.1   dante 		 * device's 'wdtr_able' bit and write the new value to the
   2280      1.1   dante 		 * microcode.
   2281      1.1   dante 		 */
   2282  1.5.2.1  bouyer #ifdef SCSI_ADW_WDTR_DISABLE
   2283  1.5.2.1  bouyer 	if(!(tidmask & SCSI_ADW_WDTR_DISABLE))
   2284  1.5.2.1  bouyer #endif /* SCSI_ADW_WDTR_DISABLE */
   2285  1.5.2.1  bouyer 		if ((sc->wdtr_able & tidmask) && (inq->flags3 & SID_WBus16)) {
   2286  1.5.2.1  bouyer 			ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE,
   2287  1.5.2.1  bouyer 					cfg_word);
   2288      1.2   dante 			if ((cfg_word & tidmask) == 0) {
   2289      1.1   dante 				cfg_word |= tidmask;
   2290  1.5.2.1  bouyer 				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_WDTR_ABLE,
   2291  1.5.2.1  bouyer 						cfg_word);
   2292      1.1   dante 
   2293      1.1   dante 				/*
   2294  1.5.2.1  bouyer 				 * Clear the microcode "SDTR negotiation" and
   2295  1.5.2.1  bouyer 				 * "WDTR negotiation" done indicators for the
   2296  1.5.2.1  bouyer 				 * target to cause it to negotiate with the new
   2297  1.5.2.1  bouyer 				 * setting set above.
   2298  1.5.2.1  bouyer 				 * WDTR when accepted causes the target to enter
   2299  1.5.2.1  bouyer 				 * asynchronous mode, so SDTR must be negotiated
   2300      1.1   dante 				 */
   2301  1.5.2.1  bouyer 				ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE,
   2302  1.5.2.1  bouyer 						cfg_word);
   2303  1.5.2.1  bouyer 				cfg_word &= ~tidmask;
   2304  1.5.2.1  bouyer 				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE,
   2305  1.5.2.1  bouyer 						cfg_word);
   2306  1.5.2.1  bouyer 				ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_WDTR_DONE,
   2307  1.5.2.1  bouyer 						cfg_word);
   2308      1.1   dante 				cfg_word &= ~tidmask;
   2309  1.5.2.1  bouyer 				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_WDTR_DONE,
   2310  1.5.2.1  bouyer 						cfg_word);
   2311      1.1   dante 			}
   2312      1.1   dante 		}
   2313  1.5.2.1  bouyer 
   2314      1.1   dante 		/*
   2315      1.1   dante 		 * Synchronous Transfers
   2316      1.1   dante 		 *
   2317      1.1   dante 		 * If the EEPROM enabled SDTR for the device and the device
   2318      1.1   dante 		 * supports synchronous transfers, then turn on the device's
   2319      1.1   dante 		 * 'sdtr_able' bit. Write the new value to the microcode.
   2320      1.1   dante 		 */
   2321  1.5.2.1  bouyer #ifdef SCSI_ADW_SDTR_DISABLE
   2322  1.5.2.1  bouyer 	if(!(tidmask & SCSI_ADW_SDTR_DISABLE))
   2323  1.5.2.1  bouyer #endif /* SCSI_ADW_SDTR_DISABLE */
   2324  1.5.2.1  bouyer 		if ((sc->sdtr_able & tidmask) && (inq->flags3 & SID_Sync)) {
   2325  1.5.2.1  bouyer 			ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE,cfg_word);
   2326      1.2   dante 			if ((cfg_word & tidmask) == 0) {
   2327      1.1   dante 				cfg_word |= tidmask;
   2328  1.5.2.1  bouyer 				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_ABLE,
   2329  1.5.2.1  bouyer 						cfg_word);
   2330      1.1   dante 
   2331      1.1   dante 				/*
   2332  1.5.2.1  bouyer 				 * Clear the microcode "SDTR negotiation"
   2333  1.5.2.1  bouyer 				 * done indicator for the target to cause it
   2334      1.2   dante 				 * to negotiate with the new setting set above.
   2335      1.1   dante 				 */
   2336  1.5.2.1  bouyer 				ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE,
   2337  1.5.2.1  bouyer 						cfg_word);
   2338      1.1   dante 				cfg_word &= ~tidmask;
   2339  1.5.2.1  bouyer 				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_SDTR_DONE,
   2340  1.5.2.1  bouyer 						cfg_word);
   2341  1.5.2.1  bouyer 			}
   2342  1.5.2.1  bouyer 		}
   2343  1.5.2.1  bouyer 		/*
   2344  1.5.2.1  bouyer 		 * If the Inquiry data included enough space for the SPI-3
   2345  1.5.2.1  bouyer 		 * Clocking field, then check if DT mode is supported.
   2346  1.5.2.1  bouyer 		 */
   2347  1.5.2.1  bouyer 		if (sc->chip_type == ADW_CHIP_ASC38C1600 &&
   2348  1.5.2.1  bouyer 		   (scsiq->cdb[4] >= 57 ||
   2349  1.5.2.1  bouyer 		   (scsiq->cdb[4] - scsiq->data_cnt) >= 57)) {
   2350  1.5.2.1  bouyer 			/*
   2351  1.5.2.1  bouyer 			 * PPR (Parallel Protocol Request) Capable
   2352  1.5.2.1  bouyer 			 *
   2353  1.5.2.1  bouyer 			 * If the device supports DT mode, then it must be
   2354  1.5.2.1  bouyer 			 * PPR capable.
   2355  1.5.2.1  bouyer 			 * The PPR message will be used in place of the SDTR
   2356  1.5.2.1  bouyer 			 * and WDTR messages to negotiate synchronous speed
   2357  1.5.2.1  bouyer 			 * and offset, transfer width, and protocol options.
   2358  1.5.2.1  bouyer 			 */
   2359  1.5.2.1  bouyer 			if((inq->flags4 & SID_Clocking) & SID_CLOCKING_DT_ONLY){
   2360  1.5.2.1  bouyer 				ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_PPR_ABLE,
   2361  1.5.2.1  bouyer 						sc->ppr_able);
   2362  1.5.2.1  bouyer 				sc->ppr_able |= tidmask;
   2363  1.5.2.1  bouyer 				ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_PPR_ABLE,
   2364  1.5.2.1  bouyer 						sc->ppr_able);
   2365      1.1   dante 			}
   2366      1.1   dante 		}
   2367  1.5.2.1  bouyer 
   2368      1.1   dante 		/*
   2369  1.5.2.1  bouyer 		 * If the EEPROM enabled Tag Queuing for the device and the
   2370  1.5.2.1  bouyer 		 * device supports Tag Queueing, then turn on the device's
   2371      1.1   dante 		 * 'tagqng_enable' bit in the microcode and set the microcode
   2372  1.5.2.1  bouyer 		 * maximum command count to the ADV_DVC_VAR 'max_dvc_qng'
   2373      1.1   dante 		 * value.
   2374      1.1   dante 		 *
   2375      1.1   dante 		 * Tag Queuing is disabled for the BIOS which runs in polled
   2376      1.1   dante 		 * mode and would see no benefit from Tag Queuing. Also by
   2377      1.1   dante 		 * disabling Tag Queuing in the BIOS devices with Tag Queuing
   2378      1.1   dante 		 * bugs will at least work with the BIOS.
   2379      1.1   dante 		 */
   2380  1.5.2.1  bouyer #ifdef SCSI_ADW_TAGQ_DISABLE
   2381  1.5.2.1  bouyer 	if(!(tidmask & SCSI_ADW_TAGQ_DISABLE))
   2382  1.5.2.1  bouyer #endif /* SCSI_ADW_TAGQ_DISABLE */
   2383  1.5.2.1  bouyer 		if ((sc->tagqng_able & tidmask) && (inq->flags3 & SID_CmdQue)) {
   2384  1.5.2.1  bouyer 			ADW_READ_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE,
   2385  1.5.2.1  bouyer 					cfg_word);
   2386      1.1   dante 			cfg_word |= tidmask;
   2387  1.5.2.1  bouyer 			ADW_WRITE_WORD_LRAM(iot, ioh, ADW_MC_TAGQNG_ABLE,
   2388  1.5.2.1  bouyer 					cfg_word);
   2389  1.5.2.1  bouyer 
   2390      1.1   dante 			ADW_WRITE_BYTE_LRAM(iot, ioh,
   2391  1.5.2.1  bouyer 					ADW_MC_NUMBER_OF_MAX_CMD + tid,
   2392  1.5.2.1  bouyer 					sc->max_dvc_qng);
   2393      1.1   dante 		}
   2394      1.1   dante 	}
   2395  1.5.2.1  bouyer #endif /* FAILSAFE */
   2396      1.1   dante }
   2397      1.1   dante 
   2398  1.5.2.1  bouyer 
   2399      1.1   dante static void
   2400  1.5.2.1  bouyer AdwSleepMilliSecond(n)
   2401  1.5.2.1  bouyer u_int32_t	n;
   2402      1.1   dante {
   2403      1.1   dante 
   2404      1.1   dante 	DELAY(n * 1000);
   2405      1.1   dante }
   2406      1.1   dante 
   2407  1.5.2.1  bouyer 
   2408      1.1   dante static void
   2409  1.5.2.1  bouyer AdwDelayMicroSecond(n)
   2410  1.5.2.1  bouyer u_int32_t	n;
   2411      1.1   dante {
   2412      1.1   dante 
   2413      1.1   dante 	DELAY(n);
   2414      1.1   dante }
   2415  1.5.2.1  bouyer 
   2416