Home | History | Annotate | Line # | Download | only in eisa
uha_eisa.c revision 1.1
      1 #include <sys/types.h>
      2 #include <sys/param.h>
      3 #include <sys/device.h>
      4 #include <sys/kernel.h>
      5 #include <sys/proc.h>
      6 #include <sys/user.h>
      7 
      8 #include <machine/bus.h>
      9 #include <machine/intr.h>
     10 
     11 #include <scsi/scsi_all.h>
     12 #include <scsi/scsiconf.h>
     13 
     14 #include <dev/eisa/eisavar.h>
     15 #include <dev/eisa/eisadevs.h>
     16 
     17 #include <dev/ic/uhareg.h>
     18 #include <dev/ic/uhavar.h>
     19 
     20 #define	UHA_EISA_SLOT_OFFSET	0xc80
     21 #define	UHA_EISA_IOSIZE		0x020
     22 
     23 int	uha_eisa_match __P((struct device *, void *, void *));
     24 void	uha_eisa_attach __P((struct device *, struct device *, void *));
     25 
     26 struct cfattach uha_eisa_ca = {
     27 	sizeof(struct uha_softc), uha_eisa_match, uha_eisa_attach
     28 };
     29 
     30 #define KVTOPHYS(x)	vtophys(x)
     31 
     32 int u24_find __P((bus_chipset_tag_t, bus_io_handle_t, struct uha_softc *));
     33 void u24_start_mbox __P((struct uha_softc *, struct uha_mscp *));
     34 int u24_poll __P((struct uha_softc *, struct scsi_xfer *, int));
     35 int u24_intr __P((void *));
     36 void u24_init __P((struct uha_softc *));
     37 
     38 /*
     39  * Check the slots looking for a board we recognise
     40  * If we find one, note it's address (slot) and call
     41  * the actual probe routine to check it out.
     42  */
     43 int
     44 uha_eisa_match(parent, match, aux)
     45 	struct device *parent;
     46 	void *match, *aux;
     47 {
     48 	struct eisa_attach_args *ea = aux;
     49 	bus_chipset_tag_t bc = ea->ea_bc;
     50 	bus_io_handle_t ioh;
     51 	int rv;
     52 
     53 	/* must match one of our known ID strings */
     54 	if (strncmp(ea->ea_idstring, "USC024", 6))
     55 		return (0);
     56 
     57 	if (bus_io_map(bc, EISA_SLOT_ADDR(ea->ea_slot) + UHA_EISA_SLOT_OFFSET,
     58 	    UHA_EISA_IOSIZE, &ioh))
     59 		return (0);
     60 
     61 	rv = u24_find(bc, ioh, NULL);
     62 
     63 	bus_io_unmap(bc, ioh, UHA_EISA_IOSIZE);
     64 
     65 	return (rv);
     66 }
     67 
     68 /*
     69  * Attach all the sub-devices we can find
     70  */
     71 void
     72 uha_eisa_attach(parent, self, aux)
     73 	struct device *parent, *self;
     74 	void *aux;
     75 {
     76 	struct eisa_attach_args *ea = aux;
     77 	struct uha_softc *sc = (void *)self;
     78 	bus_chipset_tag_t bc = ea->ea_bc;
     79 	bus_io_handle_t ioh;
     80 	eisa_chipset_tag_t ec = ea->ea_ec;
     81 	eisa_intr_handle_t ih;
     82 	const char *model, *intrstr;
     83 
     84 	if (!strncmp(ea->ea_idstring, "USC024", 6))
     85 		model = EISA_PRODUCT_USC0240;
     86 	else
     87 		model = "unknown model!";
     88 	printf(": %s\n", model);
     89 
     90 	if (bus_io_map(bc, EISA_SLOT_ADDR(ea->ea_slot) + UHA_EISA_SLOT_OFFSET,
     91 	    UHA_EISA_IOSIZE, &ioh))
     92 		panic("uha_attach: could not map I/O addresses");
     93 
     94 	sc->sc_bc = bc;
     95 	sc->sc_ioh = ioh;
     96 	if (!u24_find(bc, ioh, sc))
     97 		panic("uha_attach: u24_find failed!");
     98 
     99 	if (eisa_intr_map(ec, sc->sc_irq, &ih)) {
    100 		printf("%s: couldn't map interrupt (%d)\n",
    101 		    sc->sc_dev.dv_xname, sc->sc_irq);
    102 		return;
    103 	}
    104 	intrstr = eisa_intr_string(ec, ih);
    105 	sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
    106 	    u24_intr, sc);
    107 	if (sc->sc_ih == NULL) {
    108 		printf("%s: couldn't establish interrupt",
    109 		    sc->sc_dev.dv_xname);
    110 		if (intrstr != NULL)
    111 			printf(" at %s", intrstr);
    112 		printf("\n");
    113 		return;
    114 	}
    115 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    116 
    117 	/* Save function pointers for later use. */
    118 	sc->start_mbox = u24_start_mbox;
    119 	sc->poll = u24_poll;
    120 	sc->init = u24_init;
    121 
    122 	uha_attach(sc);
    123 }
    124 
    125 int
    126 u24_find(bc, ioh, sc)
    127 	bus_chipset_tag_t bc;
    128 	bus_io_handle_t ioh;
    129 	struct uha_softc *sc;
    130 {
    131 	u_int8_t config0, config1, config2;
    132 	int irq, drq;
    133 	int resetcount = 4000;	/* 4 secs? */
    134 
    135 	config0 = bus_io_read_1(bc, ioh, U24_CONFIG + 0);
    136 	config1 = bus_io_read_1(bc, ioh, U24_CONFIG + 1);
    137 	config2 = bus_io_read_1(bc, ioh, U24_CONFIG + 2);
    138 	if ((config0 & U24_MAGIC1) == 0 ||
    139 	    (config1 & U24_MAGIC2) == 0)
    140 		return (0);
    141 
    142 	drq = -1;
    143 
    144 	switch (config0 & U24_IRQ_MASK) {
    145 	case U24_IRQ10:
    146 		irq = 10;
    147 		break;
    148 	case U24_IRQ11:
    149 		irq = 11;
    150 		break;
    151 	case U24_IRQ14:
    152 		irq = 14;
    153 		break;
    154 	case U24_IRQ15:
    155 		irq = 15;
    156 		break;
    157 	default:
    158 		printf("u24_find: illegal irq setting %x\n",
    159 		    config0 & U24_IRQ_MASK);
    160 		return (0);
    161 	}
    162 
    163 	bus_io_write_1(bc, ioh, U24_LINT, UHA_ASRST);
    164 
    165 	while (--resetcount) {
    166 		if (bus_io_read_1(bc, ioh, U24_LINT))
    167 			break;
    168 		delay(1000);	/* 1 mSec per loop */
    169 	}
    170 	if (!resetcount) {
    171 		printf("u24_find: board timed out during reset\n");
    172 		return (0);
    173 	}
    174 
    175 	/* if we want to fill in softc, do so now */
    176 	if (sc != NULL) {
    177 		sc->sc_irq = irq;
    178 		sc->sc_drq = drq;
    179 		sc->sc_scsi_dev = config2 & U24_HOSTID_MASK;
    180 	}
    181 
    182 	return (1);
    183 }
    184 
    185 void
    186 u24_start_mbox(sc, mscp)
    187 	struct uha_softc *sc;
    188 	struct uha_mscp *mscp;
    189 {
    190 	bus_chipset_tag_t bc = sc->sc_bc;
    191 	bus_io_handle_t ioh = sc->sc_ioh;
    192 	int spincount = 100000;	/* 1s should be enough */
    193 
    194 	while (--spincount) {
    195 		if ((bus_io_read_1(bc, ioh, U24_LINT) & U24_LDIP) == 0)
    196 			break;
    197 		delay(100);
    198 	}
    199 	if (!spincount) {
    200 		printf("%s: uha_start_mbox, board not responding\n",
    201 		    sc->sc_dev.dv_xname);
    202 		Debugger();
    203 	}
    204 
    205 	bus_io_write_4(bc, ioh, U24_OGMPTR, KVTOPHYS(mscp));
    206 	if (mscp->flags & MSCP_ABORT)
    207 		bus_io_write_1(bc, ioh, U24_OGMCMD, 0x80);
    208 	else
    209 		bus_io_write_1(bc, ioh, U24_OGMCMD, 0x01);
    210 	bus_io_write_1(bc, ioh, U24_LINT, U24_OGMFULL);
    211 
    212 	if ((mscp->xs->flags & SCSI_POLL) == 0)
    213 		timeout(uha_timeout, mscp, (mscp->timeout * hz) / 1000);
    214 }
    215 
    216 int
    217 u24_poll(sc, xs, count)
    218 	struct uha_softc *sc;
    219 	struct scsi_xfer *xs;
    220 	int count;
    221 {
    222 	bus_chipset_tag_t bc = sc->sc_bc;
    223 	bus_io_handle_t ioh = sc->sc_ioh;
    224 
    225 	while (count) {
    226 		/*
    227 		 * If we had interrupts enabled, would we
    228 		 * have got an interrupt?
    229 		 */
    230 		if (bus_io_read_1(bc, ioh, U24_SINT) & U24_SDIP)
    231 			u24_intr(sc);
    232 		if (xs->flags & ITSDONE)
    233 			return (0);
    234 		delay(1000);
    235 		count--;
    236 	}
    237 	return (1);
    238 }
    239 
    240 int
    241 u24_intr(arg)
    242 	void *arg;
    243 {
    244 	struct uha_softc *sc = arg;
    245 	bus_chipset_tag_t bc = sc->sc_bc;
    246 	bus_io_handle_t ioh = sc->sc_ioh;
    247 	struct uha_mscp *mscp;
    248 	u_char uhastat;
    249 	u_long mboxval;
    250 
    251 #ifdef	UHADEBUG
    252 	printf("%s: uhaintr ", sc->sc_dev.dv_xname);
    253 #endif /*UHADEBUG */
    254 
    255 	if ((bus_io_read_1(bc, ioh, U24_SINT) & U24_SDIP) == 0)
    256 		return (0);
    257 
    258 	for (;;) {
    259 		/*
    260 		 * First get all the information and then
    261 		 * acknowledge the interrupt
    262 		 */
    263 		uhastat = bus_io_read_1(bc, ioh, U24_SINT);
    264 		mboxval = bus_io_read_4(bc, ioh, U24_ICMPTR);
    265 		bus_io_write_1(bc, ioh, U24_SINT, U24_ICM_ACK);
    266 		bus_io_write_1(bc, ioh, U24_ICMCMD, 0);
    267 
    268 #ifdef	UHADEBUG
    269 		printf("status = 0x%x ", uhastat);
    270 #endif /*UHADEBUG*/
    271 
    272 		/*
    273 		 * Process the completed operation
    274 		 */
    275 		mscp = uha_mscp_phys_kv(sc, mboxval);
    276 		if (!mscp) {
    277 			printf("%s: BAD MSCP RETURNED!\n",
    278 			    sc->sc_dev.dv_xname);
    279 			continue;	/* whatever it was, it'll timeout */
    280 		}
    281 		untimeout(uha_timeout, mscp);
    282 		uha_done(sc, mscp);
    283 
    284 		if ((bus_io_read_1(bc, ioh, U24_SINT) & U24_SDIP) == 0)
    285 			return (1);
    286 	}
    287 }
    288 
    289 void
    290 u24_init(sc)
    291 	struct uha_softc *sc;
    292 {
    293 	bus_chipset_tag_t bc = sc->sc_bc;
    294 	bus_io_handle_t ioh = sc->sc_ioh;
    295 
    296 	/* free OGM and ICM */
    297 	bus_io_write_1(bc, ioh, U24_OGMCMD, 0);
    298 	bus_io_write_1(bc, ioh, U24_ICMCMD, 0);
    299 	/* make sure interrupts are enabled */
    300 #ifdef UHADEBUG
    301 	printf("u24_init: lmask=%02x, smask=%02x\n",
    302 	    bus_io_read_1(bc, ioh, U24_LMASK),
    303 	    bus_io_read_1(bc, ioh, U24_SMASK));
    304 #endif
    305 	bus_io_write_1(bc, ioh, U24_LMASK, 0xd2);	/* XXX */
    306 	bus_io_write_1(bc, ioh, U24_SMASK, 0x92);	/* XXX */
    307 }
    308