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