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