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