Home | History | Annotate | Line # | Download | only in eisa
uha_eisa.c revision 1.16
      1 /*	$NetBSD: uha_eisa.c,v 1.16 2001/11/13 12:47: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_eisa.c,v 1.16 2001/11/13 12:47: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/eisa/eisavar.h>
     60 #include <dev/eisa/eisadevs.h>
     61 
     62 #include <dev/ic/uhareg.h>
     63 #include <dev/ic/uhavar.h>
     64 
     65 #define	UHA_EISA_SLOT_OFFSET	0xc80
     66 #define	UHA_EISA_IOSIZE		0x020
     67 
     68 int	uha_eisa_match __P((struct device *, struct cfdata *, void *));
     69 void	uha_eisa_attach __P((struct device *, struct device *, void *));
     70 
     71 struct cfattach uha_eisa_ca = {
     72 	sizeof(struct uha_softc), uha_eisa_match, uha_eisa_attach
     73 };
     74 
     75 #ifndef	DDB
     76 #define Debugger() panic("should call debugger here (uha_eisa.c)")
     77 #endif /* ! DDB */
     78 
     79 int	u24_find __P((bus_space_tag_t, bus_space_handle_t,
     80 	    struct uha_probe_data *));
     81 void	u24_start_mbox __P((struct uha_softc *, struct uha_mscp *));
     82 int	u24_poll __P((struct uha_softc *, struct scsipi_xfer *, int));
     83 int	u24_intr __P((void *));
     84 void	u24_init __P((struct uha_softc *));
     85 
     86 /*
     87  * Check the slots looking for a board we recognise
     88  * If we find one, note it's address (slot) and call
     89  * the actual probe routine to check it out.
     90  */
     91 int
     92 uha_eisa_match(parent, match, aux)
     93 	struct device *parent;
     94 	struct cfdata *match;
     95 	void *aux;
     96 {
     97 	struct eisa_attach_args *ea = aux;
     98 	bus_space_tag_t iot = ea->ea_iot;
     99 	bus_space_handle_t ioh;
    100 	int rv;
    101 
    102 	/* must match one of our known ID strings */
    103 	if (strncmp(ea->ea_idstring, "USC024", 6))
    104 		return (0);
    105 
    106 	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) +
    107 	    UHA_EISA_SLOT_OFFSET, UHA_EISA_IOSIZE, 0, &ioh))
    108 		return (0);
    109 
    110 	rv = u24_find(iot, ioh, NULL);
    111 
    112 	bus_space_unmap(iot, ioh, UHA_EISA_IOSIZE);
    113 
    114 	return (rv);
    115 }
    116 
    117 /*
    118  * Attach all the sub-devices we can find
    119  */
    120 void
    121 uha_eisa_attach(parent, self, aux)
    122 	struct device *parent, *self;
    123 	void *aux;
    124 {
    125 	struct eisa_attach_args *ea = aux;
    126 	struct uha_softc *sc = (void *)self;
    127 	bus_space_tag_t iot = ea->ea_iot;
    128 	bus_dma_tag_t dmat = ea->ea_dmat;
    129 	bus_space_handle_t ioh;
    130 	struct uha_probe_data upd;
    131 	eisa_chipset_tag_t ec = ea->ea_ec;
    132 	eisa_intr_handle_t ih;
    133 	const char *model, *intrstr;
    134 
    135 	if (!strncmp(ea->ea_idstring, "USC024", 6))
    136 		model = EISA_PRODUCT_USC0240;
    137 	else
    138 		model = "unknown model!";
    139 	printf(": %s\n", model);
    140 
    141 	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) +
    142 	    UHA_EISA_SLOT_OFFSET, UHA_EISA_IOSIZE, 0, &ioh))
    143 		panic("uha_eisa_attach: could not map I/O addresses");
    144 
    145 	sc->sc_iot = iot;
    146 	sc->sc_ioh = ioh;
    147 	sc->sc_dmat = dmat;
    148 	if (!u24_find(iot, ioh, &upd))
    149 		panic("uha_eisa_attach: u24_find failed!");
    150 
    151 	sc->sc_dmaflags = 0;
    152 
    153 	if (eisa_intr_map(ec, upd.sc_irq, &ih)) {
    154 		printf("%s: couldn't map interrupt (%d)\n",
    155 		    sc->sc_dev.dv_xname, upd.sc_irq);
    156 		return;
    157 	}
    158 	intrstr = eisa_intr_string(ec, ih);
    159 	sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
    160 	    u24_intr, sc);
    161 	if (sc->sc_ih == NULL) {
    162 		printf("%s: couldn't establish interrupt",
    163 		    sc->sc_dev.dv_xname);
    164 		if (intrstr != NULL)
    165 			printf(" at %s", intrstr);
    166 		printf("\n");
    167 		return;
    168 	}
    169 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    170 
    171 	/* Save function pointers for later use. */
    172 	sc->start_mbox = u24_start_mbox;
    173 	sc->poll = u24_poll;
    174 	sc->init = u24_init;
    175 
    176 	uha_attach(sc, &upd);
    177 }
    178 
    179 int
    180 u24_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_int8_t config0, config1, config2;
    186 	int irq, drq;
    187 	int resetcount = 4000;	/* 4 secs? */
    188 
    189 	config0 = bus_space_read_1(iot, ioh, U24_CONFIG + 0);
    190 	config1 = bus_space_read_1(iot, ioh, U24_CONFIG + 1);
    191 	config2 = bus_space_read_1(iot, ioh, U24_CONFIG + 2);
    192 	if ((config0 & U24_MAGIC1) == 0 ||
    193 	    (config1 & U24_MAGIC2) == 0)
    194 		return (0);
    195 
    196 	drq = -1;
    197 
    198 	switch (config0 & U24_IRQ_MASK) {
    199 	case U24_IRQ10:
    200 		irq = 10;
    201 		break;
    202 	case U24_IRQ11:
    203 		irq = 11;
    204 		break;
    205 	case U24_IRQ14:
    206 		irq = 14;
    207 		break;
    208 	case U24_IRQ15:
    209 		irq = 15;
    210 		break;
    211 	default:
    212 		printf("u24_find: illegal irq setting %x\n",
    213 		    config0 & U24_IRQ_MASK);
    214 		return (0);
    215 	}
    216 
    217 	bus_space_write_1(iot, ioh, U24_LINT, UHA_ASRST);
    218 
    219 	while (--resetcount) {
    220 		if (bus_space_read_1(iot, ioh, U24_LINT))
    221 			break;
    222 		delay(1000);	/* 1 mSec per loop */
    223 	}
    224 	if (!resetcount) {
    225 		printf("u24_find: board timed out during reset\n");
    226 		return (0);
    227 	}
    228 
    229 	/* if we want to fill in softc, do so now */
    230 	if (sc) {
    231 		sc->sc_irq = irq;
    232 		sc->sc_drq = drq;
    233 		sc->sc_scsi_dev = config2 & U24_HOSTID_MASK;
    234 	}
    235 
    236 	return (1);
    237 }
    238 
    239 void
    240 u24_start_mbox(sc, mscp)
    241 	struct uha_softc *sc;
    242 	struct uha_mscp *mscp;
    243 {
    244 	bus_space_tag_t iot = sc->sc_iot;
    245 	bus_space_handle_t ioh = sc->sc_ioh;
    246 	int spincount = 100000;	/* 1s should be enough */
    247 
    248 	while (--spincount) {
    249 		if ((bus_space_read_1(iot, ioh, U24_LINT) & U24_LDIP) == 0)
    250 			break;
    251 		delay(100);
    252 	}
    253 	if (!spincount) {
    254 		printf("%s: uha_start_mbox, board not responding\n",
    255 		    sc->sc_dev.dv_xname);
    256 		Debugger();
    257 	}
    258 
    259 	bus_space_write_4(iot, ioh, U24_OGMPTR,
    260 	    sc->sc_dmamap_mscp->dm_segs[0].ds_addr + UHA_MSCP_OFF(mscp));
    261 	if (mscp->flags & MSCP_ABORT)
    262 		bus_space_write_1(iot, ioh, U24_OGMCMD, 0x80);
    263 	else
    264 		bus_space_write_1(iot, ioh, U24_OGMCMD, 0x01);
    265 	bus_space_write_1(iot, ioh, U24_LINT, U24_OGMFULL);
    266 
    267 	if ((mscp->xs->xs_control & XS_CTL_POLL) == 0)
    268 		callout_reset(&mscp->xs->xs_callout,
    269 		    (mscp->timeout * hz) / 1000, uha_timeout, mscp);
    270 }
    271 
    272 int
    273 u24_poll(sc, xs, count)
    274 	struct uha_softc *sc;
    275 	struct scsipi_xfer *xs;
    276 	int count;
    277 {
    278 	bus_space_tag_t iot = sc->sc_iot;
    279 	bus_space_handle_t ioh = sc->sc_ioh;
    280 
    281 	while (count) {
    282 		/*
    283 		 * If we had interrupts enabled, would we
    284 		 * have got an interrupt?
    285 		 */
    286 		if (bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP)
    287 			u24_intr(sc);
    288 		if (xs->xs_status & XS_STS_DONE)
    289 			return (0);
    290 		delay(1000);
    291 		count--;
    292 	}
    293 	return (1);
    294 }
    295 
    296 int
    297 u24_intr(arg)
    298 	void *arg;
    299 {
    300 	struct uha_softc *sc = arg;
    301 	bus_space_tag_t iot = sc->sc_iot;
    302 	bus_space_handle_t ioh = sc->sc_ioh;
    303 	struct uha_mscp *mscp;
    304 	u_char uhastat;
    305 	u_long mboxval;
    306 
    307 #ifdef	UHADEBUG
    308 	printf("%s: uhaintr ", sc->sc_dev.dv_xname);
    309 #endif /*UHADEBUG */
    310 
    311 	if ((bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP) == 0)
    312 		return (0);
    313 
    314 	for (;;) {
    315 		/*
    316 		 * First get all the information and then
    317 		 * acknowledge the interrupt
    318 		 */
    319 		uhastat = bus_space_read_1(iot, ioh, U24_SINT);
    320 		mboxval = bus_space_read_4(iot, ioh, U24_ICMPTR);
    321 		bus_space_write_1(iot, ioh, U24_SINT, U24_ICM_ACK);
    322 		bus_space_write_1(iot, ioh, U24_ICMCMD, 0);
    323 
    324 #ifdef	UHADEBUG
    325 		printf("status = 0x%x ", uhastat);
    326 #endif /*UHADEBUG*/
    327 
    328 		/*
    329 		 * Process the completed operation
    330 		 */
    331 		mscp = uha_mscp_phys_kv(sc, mboxval);
    332 		if (!mscp) {
    333 			printf("%s: BAD MSCP RETURNED!\n",
    334 			    sc->sc_dev.dv_xname);
    335 			continue;	/* whatever it was, it'll timeout */
    336 		}
    337 		callout_stop(&mscp->xs->xs_callout);
    338 		uha_done(sc, mscp);
    339 
    340 		if ((bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP) == 0)
    341 			return (1);
    342 	}
    343 }
    344 
    345 void
    346 u24_init(sc)
    347 	struct uha_softc *sc;
    348 {
    349 	bus_space_tag_t iot = sc->sc_iot;
    350 	bus_space_handle_t ioh = sc->sc_ioh;
    351 
    352 	/* free OGM and ICM */
    353 	bus_space_write_1(iot, ioh, U24_OGMCMD, 0);
    354 	bus_space_write_1(iot, ioh, U24_ICMCMD, 0);
    355 	/* make sure interrupts are enabled */
    356 #ifdef UHADEBUG
    357 	printf("u24_init: lmask=%02x, smask=%02x\n",
    358 	    bus_space_read_1(iot, ioh, U24_LMASK),
    359 	    bus_space_read_1(iot, ioh, U24_SMASK));
    360 #endif
    361 	bus_space_write_1(iot, ioh, U24_LMASK, 0xd2);	/* XXX */
    362 	bus_space_write_1(iot, ioh, U24_SMASK, 0x92);	/* XXX */
    363 }
    364