Home | History | Annotate | Line # | Download | only in isa
uha_isa.c revision 1.15
      1 /*	$NetBSD: uha_isa.c,v 1.15 1998/06/25 19:18:06 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1996, 1997 Charles M. Hannum.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Charles M. Hannum.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/device.h>
     36 #include <sys/kernel.h>
     37 #include <sys/proc.h>
     38 #include <sys/user.h>
     39 
     40 #include <machine/bus.h>
     41 #include <machine/intr.h>
     42 
     43 #include <dev/scsipi/scsi_all.h>
     44 #include <dev/scsipi/scsipi_all.h>
     45 #include <dev/scsipi/scsiconf.h>
     46 
     47 #include <dev/isa/isavar.h>
     48 #include <dev/isa/isadmavar.h>
     49 
     50 #include <dev/ic/uhareg.h>
     51 #include <dev/ic/uhavar.h>
     52 
     53 #define	UHA_ISA_IOSIZE	16
     54 
     55 int	uha_isa_probe __P((struct device *, struct cfdata *, void *));
     56 void	uha_isa_attach __P((struct device *, struct device *, void *));
     57 
     58 struct cfattach uha_isa_ca = {
     59 	sizeof(struct uha_softc), uha_isa_probe, uha_isa_attach
     60 };
     61 
     62 #ifndef	DDB
     63 #define Debugger() panic("should call debugger here (uha_isa.c)")
     64 #endif /* ! DDB */
     65 
     66 int	u14_find __P((bus_space_tag_t, bus_space_handle_t,
     67 	    struct uha_probe_data *));
     68 void	u14_start_mbox __P((struct uha_softc *, struct uha_mscp *));
     69 int	u14_poll __P((struct uha_softc *, struct scsipi_xfer *, int));
     70 int	u14_intr __P((void *));
     71 void	u14_init __P((struct uha_softc *));
     72 
     73 /*
     74  * Check the slots looking for a board we recognise
     75  * If we find one, note it's address (slot) and call
     76  * the actual probe routine to check it out.
     77  */
     78 int
     79 uha_isa_probe(parent, match, aux)
     80 	struct device *parent;
     81 	struct cfdata *match;
     82 	void *aux;
     83 {
     84 	struct isa_attach_args *ia = aux;
     85 	bus_space_tag_t iot = ia->ia_iot;
     86 	bus_space_handle_t ioh;
     87 	struct uha_probe_data upd;
     88 	int rv;
     89 
     90 	/* Disallow wildcarded i/o address. */
     91 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
     92 		return (0);
     93 
     94 	if (bus_space_map(iot, ia->ia_iobase, UHA_ISA_IOSIZE, 0, &ioh))
     95 		return (0);
     96 
     97 	rv = u14_find(iot, ioh, &upd);
     98 
     99 	bus_space_unmap(iot, ioh, UHA_ISA_IOSIZE);
    100 
    101 	if (rv) {
    102 		if (ia->ia_irq != -1 && ia->ia_irq != upd.sc_irq)
    103 			return (0);
    104 		if (ia->ia_drq != -1 && ia->ia_drq != upd.sc_drq)
    105 			return (0);
    106 		ia->ia_irq = upd.sc_irq;
    107 		ia->ia_drq = upd.sc_drq;
    108 		ia->ia_msize = 0;
    109 		ia->ia_iosize = UHA_ISA_IOSIZE;
    110 	}
    111 	return (rv);
    112 }
    113 
    114 /*
    115  * Attach all the sub-devices we can find
    116  */
    117 void
    118 uha_isa_attach(parent, self, aux)
    119 	struct device *parent, *self;
    120 	void *aux;
    121 {
    122 	struct isa_attach_args *ia = aux;
    123 	struct uha_softc *sc = (void *)self;
    124 	bus_space_tag_t iot = ia->ia_iot;
    125 	bus_dma_tag_t dmat = ia->ia_dmat;
    126 	bus_space_handle_t ioh;
    127 	struct uha_probe_data upd;
    128 	isa_chipset_tag_t ic = ia->ia_ic;
    129 	int error;
    130 
    131 	printf("\n");
    132 
    133 	if (bus_space_map(iot, ia->ia_iobase, UHA_ISA_IOSIZE, 0, &ioh)) {
    134 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    135 		return;
    136 	}
    137 
    138 	sc->sc_iot = iot;
    139 	sc->sc_ioh = ioh;
    140 	sc->sc_dmat = dmat;
    141 	if (!u14_find(iot, ioh, &upd)) {
    142 		printf("%s: u14_find failed\n", sc->sc_dev.dv_xname);
    143 		return;
    144 	}
    145 
    146 	if (upd.sc_drq != -1) {
    147 		sc->sc_dmaflags = 0;
    148 		if ((error = isa_dmacascade(ic, upd.sc_drq)) != 0) {
    149 			printf("%s: unable to cascade DRQ, error = %d\n",
    150 			    sc->sc_dev.dv_xname, error);
    151 			return;
    152 		}
    153 	} else {
    154 		/*
    155 		 * We have a VLB controller, and can do 32-bit DMA.
    156 		 */
    157 		sc->sc_dmaflags = ISABUS_DMA_32BIT;
    158 	}
    159 
    160 	sc->sc_ih = isa_intr_establish(ic, upd.sc_irq, IST_EDGE, IPL_BIO,
    161 	    u14_intr, sc);
    162 	if (sc->sc_ih == NULL) {
    163 		printf("%s: couldn't establish interrupt\n",
    164 		    sc->sc_dev.dv_xname);
    165 		return;
    166 	}
    167 
    168 	/* Save function pointers for later use. */
    169 	sc->start_mbox = u14_start_mbox;
    170 	sc->poll = u14_poll;
    171 	sc->init = u14_init;
    172 
    173 	uha_attach(sc, &upd);
    174 }
    175 
    176 /*
    177  * Start the board, ready for normal operation
    178  */
    179 int
    180 u14_find(iot, ioh, sc)
    181 	bus_space_tag_t iot;
    182 	bus_space_handle_t ioh;
    183 	struct uha_probe_data *sc;
    184 {
    185 	u_int16_t model, config;
    186 	int irq, drq;
    187 	int resetcount = 4000;	/* 4 secs? */
    188 
    189 	model = (bus_space_read_1(iot, ioh, U14_ID + 0) << 8) |
    190 		(bus_space_read_1(iot, ioh, U14_ID + 1) << 0);
    191 	if ((model & 0xfff0) != 0x5640)
    192 		return (0);
    193 
    194 	config = (bus_space_read_1(iot, ioh, U14_CONFIG + 0) << 8) |
    195 		 (bus_space_read_1(iot, ioh, U14_CONFIG + 1) << 0);
    196 
    197 	switch (model & 0x000f) {
    198 	case 0x0000:
    199 		switch (config & U14_DMA_MASK) {
    200 		case U14_DMA_CH5:
    201 			drq = 5;
    202 			break;
    203 		case U14_DMA_CH6:
    204 			drq = 6;
    205 			break;
    206 		case U14_DMA_CH7:
    207 			drq = 7;
    208 			break;
    209 		default:
    210 			printf("u14_find: illegal drq setting %x\n",
    211 			    config & U14_DMA_MASK);
    212 			return (0);
    213 		}
    214 		break;
    215 	case 0x0001:
    216 		/* This is a 34f, and doesn't need an ISA DMA channel. */
    217 		drq = -1;
    218 		break;
    219 	default:
    220 		printf("u14_find: unknown model %x\n", model);
    221 		return (0);
    222 	}
    223 
    224 	switch (config & U14_IRQ_MASK) {
    225 	case U14_IRQ10:
    226 		irq = 10;
    227 		break;
    228 	case U14_IRQ11:
    229 		irq = 11;
    230 		break;
    231 	case U14_IRQ14:
    232 		irq = 14;
    233 		break;
    234 	case U14_IRQ15:
    235 		irq = 15;
    236 		break;
    237 	default:
    238 		printf("u14_find: illegal irq setting %x\n",
    239 		    config & U14_IRQ_MASK);
    240 		return (0);
    241 	}
    242 
    243 	bus_space_write_1(iot, ioh, U14_LINT, UHA_ASRST);
    244 
    245 	while (--resetcount) {
    246 		if (bus_space_read_1(iot, ioh, U14_LINT))
    247 			break;
    248 		delay(1000);	/* 1 mSec per loop */
    249 	}
    250 	if (!resetcount) {
    251 		printf("u14_find: board timed out during reset\n");
    252 		return (0);
    253 	}
    254 
    255 	/* if we want to fill in softc, do so now */
    256 	if (sc) {
    257 		sc->sc_irq = irq;
    258 		sc->sc_drq = drq;
    259 		sc->sc_scsi_dev = config & U14_HOSTID_MASK;
    260 	}
    261 
    262 	return (1);
    263 }
    264 
    265 /*
    266  * Function to send a command out through a mailbox
    267  */
    268 void
    269 u14_start_mbox(sc, mscp)
    270 	struct uha_softc *sc;
    271 	struct uha_mscp *mscp;
    272 {
    273 	bus_space_tag_t iot = sc->sc_iot;
    274 	bus_space_handle_t ioh = sc->sc_ioh;
    275 	int spincount = 100000;	/* 1s should be enough */
    276 
    277 	while (--spincount) {
    278 		if ((bus_space_read_1(iot, ioh, U14_LINT) & U14_LDIP) == 0)
    279 			break;
    280 		delay(100);
    281 	}
    282 	if (!spincount) {
    283 		printf("%s: uha_start_mbox, board not responding\n",
    284 		    sc->sc_dev.dv_xname);
    285 		Debugger();
    286 	}
    287 
    288 	bus_space_write_4(iot, ioh, U14_OGMPTR,
    289 	    sc->sc_dmamap_mscp->dm_segs[0].ds_addr + UHA_MSCP_OFF(mscp));
    290 	if (mscp->flags & MSCP_ABORT)
    291 		bus_space_write_1(iot, ioh, U14_LINT, U14_ABORT);
    292 	else
    293 		bus_space_write_1(iot, ioh, U14_LINT, U14_OGMFULL);
    294 
    295 	if ((mscp->xs->flags & SCSI_POLL) == 0)
    296 		timeout(uha_timeout, mscp, (mscp->timeout * hz) / 1000);
    297 }
    298 
    299 /*
    300  * Function to poll for command completion when in poll mode.
    301  *
    302  *	wait = timeout in msec
    303  */
    304 int
    305 u14_poll(sc, xs, count)
    306 	struct uha_softc *sc;
    307 	struct scsipi_xfer *xs;
    308 	int count;
    309 {
    310 	bus_space_tag_t iot = sc->sc_iot;
    311 	bus_space_handle_t ioh = sc->sc_ioh;
    312 
    313 	while (count) {
    314 		/*
    315 		 * If we had interrupts enabled, would we
    316 		 * have got an interrupt?
    317 		 */
    318 		if (bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP)
    319 			u14_intr(sc);
    320 		if (xs->flags & ITSDONE)
    321 			return (0);
    322 		delay(1000);
    323 		count--;
    324 	}
    325 	return (1);
    326 }
    327 
    328 /*
    329  * Catch an interrupt from the adaptor
    330  */
    331 int
    332 u14_intr(arg)
    333 	void *arg;
    334 {
    335 	struct uha_softc *sc = arg;
    336 	bus_space_tag_t iot = sc->sc_iot;
    337 	bus_space_handle_t ioh = sc->sc_ioh;
    338 	struct uha_mscp *mscp;
    339 	u_char uhastat;
    340 	u_long mboxval;
    341 
    342 #ifdef	UHADEBUG
    343 	printf("%s: uhaintr ", sc->sc_dev.dv_xname);
    344 #endif /*UHADEBUG */
    345 
    346 	if ((bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP) == 0)
    347 		return (0);
    348 
    349 	for (;;) {
    350 		/*
    351 		 * First get all the information and then
    352 		 * acknowledge the interrupt
    353 		 */
    354 		uhastat = bus_space_read_1(iot, ioh, U14_SINT);
    355 		mboxval = bus_space_read_4(iot, ioh, U14_ICMPTR);
    356 		/* XXX Send an ABORT_ACK instead? */
    357 		bus_space_write_1(iot, ioh, U14_SINT, U14_ICM_ACK);
    358 
    359 #ifdef	UHADEBUG
    360 		printf("status = 0x%x ", uhastat);
    361 #endif /*UHADEBUG*/
    362 
    363 		/*
    364 		 * Process the completed operation
    365 		 */
    366 		mscp = uha_mscp_phys_kv(sc, mboxval);
    367 		if (!mscp) {
    368 			printf("%s: BAD MSCP RETURNED!\n",
    369 			    sc->sc_dev.dv_xname);
    370 			continue;	/* whatever it was, it'll timeout */
    371 		}
    372 
    373 		untimeout(uha_timeout, mscp);
    374 		uha_done(sc, mscp);
    375 
    376 		if ((bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP) == 0)
    377 			return (1);
    378 	}
    379 }
    380 
    381 void
    382 u14_init(sc)
    383 	struct uha_softc *sc;
    384 {
    385 	bus_space_tag_t iot = sc->sc_iot;
    386 	bus_space_handle_t ioh = sc->sc_ioh;
    387 
    388 	/* make sure interrupts are enabled */
    389 #ifdef UHADEBUG
    390 	printf("u14_init: lmask=%02x, smask=%02x\n",
    391 	    bus_space_read_1(iot, ioh, U14_LMASK),
    392 	    bus_space_read_1(iot, ioh, U14_SMASK));
    393 #endif
    394 	bus_space_write_1(iot, ioh, U14_LMASK, 0xd1);	/* XXX */
    395 	bus_space_write_1(iot, ioh, U14_SMASK, 0x91);	/* XXX */
    396 }
    397