Home | History | Annotate | Line # | Download | only in isa
uha_isa.c revision 1.11
      1 /*	$NetBSD: uha_isa.c,v 1.11 1997/10/20 18:43:20 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 #ifdef __BROKEN_INDIRECT_CONFIG
     56 int	uha_isa_probe __P((struct device *, void *, void *));
     57 #else
     58 int	uha_isa_probe __P((struct device *, struct cfdata *, void *));
     59 #endif
     60 void	uha_isa_attach __P((struct device *, struct device *, void *));
     61 
     62 struct cfattach uha_isa_ca = {
     63 	sizeof(struct uha_softc), uha_isa_probe, uha_isa_attach
     64 };
     65 
     66 #ifndef	DDB
     67 #define Debugger() panic("should call debugger here (uha_isa.c)")
     68 #endif /* ! DDB */
     69 
     70 int	u14_find __P((bus_space_tag_t, bus_space_handle_t,
     71 	    struct uha_probe_data *));
     72 void	u14_start_mbox __P((struct uha_softc *, struct uha_mscp *));
     73 int	u14_poll __P((struct uha_softc *, struct scsipi_xfer *, int));
     74 int	u14_intr __P((void *));
     75 void	u14_init __P((struct uha_softc *));
     76 
     77 /*
     78  * Check the slots looking for a board we recognise
     79  * If we find one, note it's address (slot) and call
     80  * the actual probe routine to check it out.
     81  */
     82 int
     83 uha_isa_probe(parent, match, aux)
     84 	struct device *parent;
     85 #ifdef __BROKEN_INDIRECT_CONFIG
     86 	void *match;
     87 #else
     88 	struct cfdata *match;
     89 #endif
     90 	void *aux;
     91 {
     92 	struct isa_attach_args *ia = aux;
     93 	bus_space_tag_t iot = ia->ia_iot;
     94 	bus_space_handle_t ioh;
     95 	struct uha_probe_data upd;
     96 	int rv;
     97 
     98 	/* Disallow wildcarded i/o address. */
     99 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
    100 		return (0);
    101 
    102 	if (bus_space_map(iot, ia->ia_iobase, UHA_ISA_IOSIZE, 0, &ioh))
    103 		return (0);
    104 
    105 	rv = u14_find(iot, ioh, &upd);
    106 
    107 	bus_space_unmap(iot, ioh, UHA_ISA_IOSIZE);
    108 
    109 	if (rv) {
    110 		if (ia->ia_irq != -1 && ia->ia_irq != upd.sc_irq)
    111 			return (0);
    112 		if (ia->ia_drq != -1 && ia->ia_drq != upd.sc_drq)
    113 			return (0);
    114 		ia->ia_irq = upd.sc_irq;
    115 		ia->ia_drq = upd.sc_drq;
    116 		ia->ia_msize = 0;
    117 		ia->ia_iosize = UHA_ISA_IOSIZE;
    118 	}
    119 	return (rv);
    120 }
    121 
    122 /*
    123  * Attach all the sub-devices we can find
    124  */
    125 void
    126 uha_isa_attach(parent, self, aux)
    127 	struct device *parent, *self;
    128 	void *aux;
    129 {
    130 	struct isa_attach_args *ia = aux;
    131 	struct uha_softc *sc = (void *)self;
    132 	bus_space_tag_t iot = ia->ia_iot;
    133 	bus_dma_tag_t dmat = ia->ia_dmat;
    134 	bus_space_handle_t ioh;
    135 	struct uha_probe_data upd;
    136 	isa_chipset_tag_t ic = ia->ia_ic;
    137 
    138 	printf("\n");
    139 
    140 	if (bus_space_map(iot, ia->ia_iobase, UHA_ISA_IOSIZE, 0, &ioh)) {
    141 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    142 		return;
    143 	}
    144 
    145 	sc->sc_iot = iot;
    146 	sc->sc_ioh = ioh;
    147 	sc->sc_dmat = dmat;
    148 	if (!u14_find(iot, ioh, &upd)) {
    149 		printf("%s: u14_find failed\n", sc->sc_dev.dv_xname);
    150 		return;
    151 	}
    152 
    153 	if (upd.sc_drq != -1) {
    154 		sc->sc_dmaflags = 0;
    155 		isa_dmacascade(parent, upd.sc_drq);
    156 	} else {
    157 		/*
    158 		 * We have a VLB controller, and can do 32-bit DMA.
    159 		 */
    160 		sc->sc_dmaflags = ISABUS_DMA_32BIT;
    161 	}
    162 
    163 	sc->sc_ih = isa_intr_establish(ic, upd.sc_irq, IST_EDGE, IPL_BIO,
    164 	    u14_intr, sc);
    165 	if (sc->sc_ih == NULL) {
    166 		printf("%s: couldn't establish interrupt\n",
    167 		    sc->sc_dev.dv_xname);
    168 		return;
    169 	}
    170 
    171 	/* Save function pointers for later use. */
    172 	sc->start_mbox = u14_start_mbox;
    173 	sc->poll = u14_poll;
    174 	sc->init = u14_init;
    175 
    176 	uha_attach(sc, &upd);
    177 }
    178 
    179 /*
    180  * Start the board, ready for normal operation
    181  */
    182 int
    183 u14_find(iot, ioh, sc)
    184 	bus_space_tag_t iot;
    185 	bus_space_handle_t ioh;
    186 	struct uha_probe_data *sc;
    187 {
    188 	u_int16_t model, config;
    189 	int irq, drq;
    190 	int resetcount = 4000;	/* 4 secs? */
    191 
    192 	model = (bus_space_read_1(iot, ioh, U14_ID + 0) << 8) |
    193 		(bus_space_read_1(iot, ioh, U14_ID + 1) << 0);
    194 	if ((model & 0xfff0) != 0x5640)
    195 		return (0);
    196 
    197 	config = (bus_space_read_1(iot, ioh, U14_CONFIG + 0) << 8) |
    198 		 (bus_space_read_1(iot, ioh, U14_CONFIG + 1) << 0);
    199 
    200 	switch (model & 0x000f) {
    201 	case 0x0000:
    202 		switch (config & U14_DMA_MASK) {
    203 		case U14_DMA_CH5:
    204 			drq = 5;
    205 			break;
    206 		case U14_DMA_CH6:
    207 			drq = 6;
    208 			break;
    209 		case U14_DMA_CH7:
    210 			drq = 7;
    211 			break;
    212 		default:
    213 			printf("u14_find: illegal drq setting %x\n",
    214 			    config & U14_DMA_MASK);
    215 			return (0);
    216 		}
    217 		break;
    218 	case 0x0001:
    219 		/* This is a 34f, and doesn't need an ISA DMA channel. */
    220 		drq = -1;
    221 		break;
    222 	default:
    223 		printf("u14_find: unknown model %x\n", model);
    224 		return (0);
    225 	}
    226 
    227 	switch (config & U14_IRQ_MASK) {
    228 	case U14_IRQ10:
    229 		irq = 10;
    230 		break;
    231 	case U14_IRQ11:
    232 		irq = 11;
    233 		break;
    234 	case U14_IRQ14:
    235 		irq = 14;
    236 		break;
    237 	case U14_IRQ15:
    238 		irq = 15;
    239 		break;
    240 	default:
    241 		printf("u14_find: illegal irq setting %x\n",
    242 		    config & U14_IRQ_MASK);
    243 		return (0);
    244 	}
    245 
    246 	bus_space_write_1(iot, ioh, U14_LINT, UHA_ASRST);
    247 
    248 	while (--resetcount) {
    249 		if (bus_space_read_1(iot, ioh, U14_LINT))
    250 			break;
    251 		delay(1000);	/* 1 mSec per loop */
    252 	}
    253 	if (!resetcount) {
    254 		printf("u14_find: board timed out during reset\n");
    255 		return (0);
    256 	}
    257 
    258 	/* if we want to fill in softc, do so now */
    259 	if (sc) {
    260 		sc->sc_irq = irq;
    261 		sc->sc_drq = drq;
    262 		sc->sc_scsi_dev = config & U14_HOSTID_MASK;
    263 	}
    264 
    265 	return (1);
    266 }
    267 
    268 /*
    269  * Function to send a command out through a mailbox
    270  */
    271 void
    272 u14_start_mbox(sc, mscp)
    273 	struct uha_softc *sc;
    274 	struct uha_mscp *mscp;
    275 {
    276 	bus_space_tag_t iot = sc->sc_iot;
    277 	bus_space_handle_t ioh = sc->sc_ioh;
    278 	int spincount = 100000;	/* 1s should be enough */
    279 
    280 	while (--spincount) {
    281 		if ((bus_space_read_1(iot, ioh, U14_LINT) & U14_LDIP) == 0)
    282 			break;
    283 		delay(100);
    284 	}
    285 	if (!spincount) {
    286 		printf("%s: uha_start_mbox, board not responding\n",
    287 		    sc->sc_dev.dv_xname);
    288 		Debugger();
    289 	}
    290 
    291 	bus_space_write_4(iot, ioh, U14_OGMPTR,
    292 	    mscp->dmamap_self->dm_segs[0].ds_addr);
    293 	if (mscp->flags & MSCP_ABORT)
    294 		bus_space_write_1(iot, ioh, U14_LINT, U14_ABORT);
    295 	else
    296 		bus_space_write_1(iot, ioh, U14_LINT, U14_OGMFULL);
    297 
    298 	if ((mscp->xs->flags & SCSI_POLL) == 0)
    299 		timeout(uha_timeout, mscp, (mscp->timeout * hz) / 1000);
    300 }
    301 
    302 /*
    303  * Function to poll for command completion when in poll mode.
    304  *
    305  *	wait = timeout in msec
    306  */
    307 int
    308 u14_poll(sc, xs, count)
    309 	struct uha_softc *sc;
    310 	struct scsipi_xfer *xs;
    311 	int count;
    312 {
    313 	bus_space_tag_t iot = sc->sc_iot;
    314 	bus_space_handle_t ioh = sc->sc_ioh;
    315 
    316 	while (count) {
    317 		/*
    318 		 * If we had interrupts enabled, would we
    319 		 * have got an interrupt?
    320 		 */
    321 		if (bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP)
    322 			u14_intr(sc);
    323 		if (xs->flags & ITSDONE)
    324 			return (0);
    325 		delay(1000);
    326 		count--;
    327 	}
    328 	return (1);
    329 }
    330 
    331 /*
    332  * Catch an interrupt from the adaptor
    333  */
    334 int
    335 u14_intr(arg)
    336 	void *arg;
    337 {
    338 	struct uha_softc *sc = arg;
    339 	bus_space_tag_t iot = sc->sc_iot;
    340 	bus_space_handle_t ioh = sc->sc_ioh;
    341 	struct uha_mscp *mscp;
    342 	u_char uhastat;
    343 	u_long mboxval;
    344 
    345 #ifdef	UHADEBUG
    346 	printf("%s: uhaintr ", sc->sc_dev.dv_xname);
    347 #endif /*UHADEBUG */
    348 
    349 	if ((bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP) == 0)
    350 		return (0);
    351 
    352 	for (;;) {
    353 		/*
    354 		 * First get all the information and then
    355 		 * acknowledge the interrupt
    356 		 */
    357 		uhastat = bus_space_read_1(iot, ioh, U14_SINT);
    358 		mboxval = bus_space_read_4(iot, ioh, U14_ICMPTR);
    359 		/* XXX Send an ABORT_ACK instead? */
    360 		bus_space_write_1(iot, ioh, U14_SINT, U14_ICM_ACK);
    361 
    362 #ifdef	UHADEBUG
    363 		printf("status = 0x%x ", uhastat);
    364 #endif /*UHADEBUG*/
    365 
    366 		/*
    367 		 * Process the completed operation
    368 		 */
    369 		mscp = uha_mscp_phys_kv(sc, mboxval);
    370 		if (!mscp) {
    371 			printf("%s: BAD MSCP RETURNED!\n",
    372 			    sc->sc_dev.dv_xname);
    373 			continue;	/* whatever it was, it'll timeout */
    374 		}
    375 
    376 		untimeout(uha_timeout, mscp);
    377 		uha_done(sc, mscp);
    378 
    379 		if ((bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP) == 0)
    380 			return (1);
    381 	}
    382 }
    383 
    384 void
    385 u14_init(sc)
    386 	struct uha_softc *sc;
    387 {
    388 	bus_space_tag_t iot = sc->sc_iot;
    389 	bus_space_handle_t ioh = sc->sc_ioh;
    390 
    391 	/* make sure interrupts are enabled */
    392 #ifdef UHADEBUG
    393 	printf("u14_init: lmask=%02x, smask=%02x\n",
    394 	    bus_space_read_1(iot, ioh, U14_LMASK),
    395 	    bus_space_read_1(iot, ioh, U14_SMASK));
    396 #endif
    397 	bus_space_write_1(iot, ioh, U14_LMASK, 0xd1);	/* XXX */
    398 	bus_space_write_1(iot, ioh, U14_SMASK, 0x91);	/* XXX */
    399 }
    400