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