Home | History | Annotate | Line # | Download | only in isa
dpt_isa.c revision 1.6
      1 /*	$NetBSD: dpt_isa.c,v 1.6 2001/11/13 08:01:12 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999, 2000, 2001 Andrew Doran <ad (at) netbsd.org>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  *
     28  */
     29 
     30 /*
     31  * ISA front-end for DPT EATA SCSI driver.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: dpt_isa.c,v 1.6 2001/11/13 08:01:12 lukem Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 #include <sys/queue.h>
     41 
     42 #include <machine/bus.h>
     43 #include <machine/intr.h>
     44 
     45 #include <dev/scsipi/scsipi_all.h>
     46 #include <dev/scsipi/scsiconf.h>
     47 
     48 #include <dev/isa/isareg.h>
     49 #include <dev/isa/isavar.h>
     50 
     51 #include <dev/isa/isadmareg.h>
     52 #include <dev/isa/isadmavar.h>
     53 
     54 #include <dev/ic/dptreg.h>
     55 #include <dev/ic/dptvar.h>
     56 
     57 #define	DPT_ISA_IOSIZE		16
     58 #define DPT_ISA_MAXCCBS		16
     59 
     60 static void	dpt_isa_attach(struct device *, struct device *, void *);
     61 static int	dpt_isa_match(struct device *, struct cfdata *, void *);
     62 static int	dpt_isa_probe(struct isa_attach_args *, int);
     63 static int	dpt_isa_wait(bus_space_handle_t, bus_space_tag_t, u_int8_t,
     64 			     u_int8_t);
     65 
     66 struct cfattach dpt_isa_ca = {
     67 	sizeof(struct dpt_softc), dpt_isa_match, dpt_isa_attach
     68 };
     69 
     70 /* Try 'less intrusive' addresses first */
     71 static const int	dpt_isa_iobases[] = { 0x230, 0x330, 0x1f0, 0x170, 0 };
     72 
     73 /*
     74  * Wait for the HBA status register to reach a specific state.
     75  */
     76 static int
     77 dpt_isa_wait(bus_space_handle_t ioh, bus_space_tag_t iot, u_int8_t mask,
     78 	     u_int8_t state)
     79 {
     80 	int ms;
     81 
     82 	for (ms = 2000 * 10; ms; ms--) {
     83 		if ((bus_space_read_1(iot, ioh, HA_STATUS) & mask) == state)
     84 			return (0);
     85 		DELAY(100);
     86 	}
     87 
     88 	return (-1);
     89 }
     90 
     91 /*
     92  * Match a supported board.
     93  */
     94 static int
     95 dpt_isa_match(struct device *parent, struct cfdata *match, void *aux)
     96 {
     97 	struct isa_attach_args *ia;
     98 	int i;
     99 
    100 	ia = aux;
    101 
    102 	if (ia->ia_iobase != ISACF_PORT_DEFAULT)
    103 		return (dpt_isa_probe(ia, ia->ia_iobase));
    104 
    105 	for (i = 0; dpt_isa_iobases[i] != 0; i++)
    106 		if (dpt_isa_probe(ia, dpt_isa_iobases[i])) {
    107 			ia->ia_iobase = dpt_isa_iobases[i];
    108 			return (1);
    109 		}
    110 
    111 	return (0);
    112 }
    113 
    114 /*
    115  * Probe for a supported board.
    116  */
    117 static int
    118 dpt_isa_probe(struct isa_attach_args *ia, int iobase)
    119 {
    120 	struct eata_cfg ec;
    121 	bus_space_handle_t ioh;
    122 	bus_space_tag_t iot;
    123 	int i, j, stat;
    124 	u_int16_t *p;
    125 
    126 	iot = ia->ia_iot;
    127 
    128 	if (bus_space_map(iot, iobase, DPT_ISA_IOSIZE, 0, &ioh) != 0)
    129 		return(0);
    130 
    131 	/*
    132 	 * Assumuing the DPT BIOS reset the board, we shouldn't need to
    133 	 * re-do it here.  The tests below should weed out non-EATA devices
    134 	 * before we start poking any registers.
    135 	 */
    136 	for (i = 1000; i; i--) {
    137 		if ((bus_space_read_1(iot, ioh, HA_STATUS) & HA_ST_READY) != 0)
    138 			break;
    139 		DELAY(2000);
    140 	}
    141 
    142 	if (i == 0)
    143 		goto bad;
    144 
    145 	while((((stat = bus_space_read_1(iot, ioh, HA_STATUS))
    146 	    != (HA_ST_READY|HA_ST_SEEK_COMPLETE))
    147 	    && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR))
    148 	    && (stat != (HA_ST_READY|HA_ST_SEEK_COMPLETE|HA_ST_ERROR|HA_ST_DRQ)))
    149 	    || (dpt_isa_wait(ioh, iot, HA_ST_BUSY, 0)))
    150 		/* RAID drives still spinning up? */
    151 		if (bus_space_read_1(iot, ioh, HA_ERROR) != 'D' ||
    152 		    bus_space_read_1(iot, ioh, HA_ERROR + 1) != 'P' ||
    153 		    bus_space_read_1(iot, ioh, HA_ERROR + 2) != 'T')
    154 			goto bad;
    155 
    156 	/*
    157 	 * At this point we can be confident that we are dealing with a DPT
    158 	 * HBA.  Issue the read-config command and wait for the data to
    159 	 * appear.  XXX We shouldn't be doing this with PIO, but it makes it
    160 	 * a lot easier as no DMA setup is required.
    161 	 */
    162 	bus_space_write_1(iot, ioh, HA_COMMAND, CP_PIO_GETCFG);
    163 	memset(&ec, 0, sizeof(ec));
    164 	i = ((int)&((struct eata_cfg *)0)->ec_cfglen +
    165 	    sizeof(ec.ec_cfglen)) >> 1;
    166 	p = (u_int16_t *)&ec;
    167 
    168 	if (dpt_isa_wait(ioh, iot, 0xFF, HA_ST_DATA_RDY))
    169 		goto bad;
    170 
    171 	/* Begin reading */
    172  	while (i--)
    173 		*p++ = bus_space_read_stream_2(iot, ioh, HA_DATA);
    174 
    175 	if ((i = ec.ec_cfglen) > (sizeof(struct eata_cfg)
    176 	    - (int)(&(((struct eata_cfg *)0L)->ec_cfglen))
    177 	    - sizeof(ec.ec_cfglen)))
    178 		i = sizeof(struct eata_cfg)
    179 		  - (int)(&(((struct eata_cfg *)0L)->ec_cfglen))
    180 		  - sizeof(ec.ec_cfglen);
    181 
    182 	j = i + (int)(&(((struct eata_cfg *)0L)->ec_cfglen)) +
    183 	    sizeof(ec.ec_cfglen);
    184 	i >>= 1;
    185 
    186 	while (i--)
    187 		*p++ = bus_space_read_stream_2(iot, ioh, HA_DATA);
    188 
    189 	/* Flush until we have read 512 bytes. */
    190 	i = (512 - j + 1) >> 1;
    191 	while (i--)
    192  		bus_space_read_stream_2(iot, ioh, HA_DATA);
    193 
    194 	/* Puke if we don't like the returned configuration data. */
    195 	if ((bus_space_read_1(iot, ioh,  HA_STATUS) & HA_ST_ERROR) != 0 ||
    196 	    memcmp(ec.ec_eatasig, "EATA", 4) != 0 ||
    197 	    (ec.ec_feat0 & (EC_F0_HBA_VALID | EC_F0_DMA_SUPPORTED)) !=
    198 	    (EC_F0_HBA_VALID | EC_F0_DMA_SUPPORTED))
    199 	    	goto bad;
    200 
    201 	/*
    202 	 * Which DMA channel to use: if it was hardwired in the kernel
    203 	 * configuration, use that value.  If the HBA told us, use that
    204 	 * value.  Otherwise, puke.
    205 	 */
    206 	if (ia->ia_drq == -1) {
    207 		int dmanum = ((ec.ec_feat1 & EC_F1_DMA_NUM_MASK) >>
    208 		    EC_F1_DMA_NUM_SHIFT);
    209 
    210 		if ((ec.ec_feat0 & EC_F0_DMA_NUM_VALID) == 0 || dmanum > 3)
    211 			goto bad;
    212 		ia->ia_drq = "\0\7\6\5"[dmanum];
    213 	}
    214 
    215 	/*
    216 	 * Which IRQ to use: if it was hardwired in the kernel configuration,
    217 	 * use that value.  Otherwise, use what the HBA told us.
    218 	 */
    219 	if (ia->ia_irq == -1)
    220 		ia->ia_irq = ((ec.ec_feat1 & EC_F1_IRQ_NUM_MASK) >>
    221 		    EC_F1_IRQ_NUM_SHIFT);
    222 
    223 	ia->ia_msize = 0;
    224 	ia->ia_iosize = DPT_ISA_IOSIZE;
    225 	bus_space_unmap(iot, ioh, DPT_ISA_IOSIZE);
    226 	return (1);
    227  bad:
    228 	bus_space_unmap(iot, ioh, DPT_ISA_IOSIZE);
    229 	return (0);
    230 }
    231 
    232 /*
    233  * Attach a matched board.
    234  */
    235 static void
    236 dpt_isa_attach(struct device *parent, struct device *self, void *aux)
    237 {
    238 	struct isa_attach_args *ia;
    239 	isa_chipset_tag_t ic;
    240 	bus_space_handle_t ioh;
    241 	bus_space_tag_t iot;
    242 	struct dpt_softc *sc;
    243 	struct eata_cfg *ec;
    244 	int error;
    245 
    246 	ia = aux;
    247 	sc = (struct dpt_softc *)self;
    248 	iot = ia->ia_iot;
    249 	ic = ia->ia_ic;
    250 
    251 	printf(": ");
    252 
    253 	if ((error = bus_space_map(iot, ia->ia_iobase, DPT_ISA_IOSIZE, 0,
    254 	    &ioh)) != 0) {
    255 		printf("can't map i/o space, error = %d\n", error);
    256 		return;
    257 	}
    258 
    259 	sc->sc_iot = iot;
    260 	sc->sc_ioh = ioh;
    261 	sc->sc_dmat = ia->ia_dmat;
    262 
    263 	if ((error = isa_dmacascade(ic, ia->ia_drq)) != 0) {
    264 		printf("unable to cascade DRQ, error = %d\n", error);
    265 		return;
    266 	}
    267 
    268 	/* Establish the interrupt. */
    269 	sc->sc_ih = isa_intr_establish(ic, ia->ia_irq, IST_EDGE, IPL_BIO,
    270 	    dpt_intr, sc);
    271 	if (sc->sc_ih == NULL) {
    272 		printf("can't establish interrupt\n");
    273 		return;
    274 	}
    275 
    276 	if (dpt_readcfg(sc)) {
    277 		printf("readcfg failed - see dpt(4)\n");
    278 		return;
    279 	}
    280 
    281 	/*
    282 	 * Now attach to the bus-independent code.  XXX We need to force
    283 	 * parameters that aren't filled in by some ISA boards.  In
    284 	 * particular, due to the limited amount of memory we have to play
    285 	 * with for DMA, clamp the number of CCBs to 16.
    286 	 */
    287 	ec = &sc->sc_ec;
    288 
    289 	if (be16toh(*(int16_t *)ec->ec_queuedepth) > DPT_ISA_MAXCCBS)
    290 		*(int16_t *)ec->ec_queuedepth = htobe16(DPT_ISA_MAXCCBS);
    291 	if (ec->ec_maxlun == 0)
    292 		ec->ec_maxlun = 7;
    293 	if ((ec->ec_feat3 & EC_F3_MAX_TARGET_MASK) >> EC_F3_MAX_TARGET_SHIFT
    294 	    == 0)
    295 		ec->ec_feat3 = (ec->ec_feat3 & ~EC_F3_MAX_TARGET_MASK) |
    296 		    (7 << EC_F3_MAX_TARGET_SHIFT);
    297 
    298 	/* Now attach to the bus-independent code. */
    299 	dpt_init(sc, NULL);
    300 }
    301