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