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